-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
124 lines (102 loc) · 3.93 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const swaggerUi = require('swagger-ui-express');
const YAML = require('yamljs');
const swaggerDocument = YAML.load('./swagger.yaml');
const sequelize = require('./util/database');
const firebaseAdmin = require('./util/send_push_notifications');
//Routers
const indexRouter = require('./routes/index');
const usersRouter = require('./routes/user');
const vendorsRouter = require('./routes/vendor');
const adminsRouter = require('./routes/admin');
//Models
const Product = require('./models/product');
const Vendor = require('./models/vendor');
const ProductCategory = require('./models/product_category');
const ProductTemplate = require('./models/product_template');
const Order = require('./models/order');
const User = require('./models/user');
const VendorType = require('./models/vendor_type');
const Notification = require('./models/notification');
const Complaint = require('./models/complaint');
const Coupon = require('./models/coupon');
///Helper to delete files
const fileHandler = require('./util/delete-file');
var app = express();
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
//exposes folders to public
app.use(express.static(path.join(__dirname, 'public')));
app.use('/images', express.static(path.join(__dirname, 'images')));
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader(
'Access-Control-Allow-Methods',
'OPTIONS, GET, POST, PUT, PATCH, DELETE'
);
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
});
app.use('/', indexRouter);
//Swagger API Docs
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use('/users', usersRouter);
app.use('/vendors', vendorsRouter);
app.use('/admins', adminsRouter);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
// error handler
app.use((err, req, res, next) => {
const status = err.statusCode || 500;
const message = err.message;
const data = err.data;
//If error's thrown delete files that got stored
if(req.files){
for(var i=0; i<req.files.length; i++){
fileHandler.deleteFile(req.files[i].path);
}
}
if(req.file){
fileHandler.deleteFile(req.file.path);
}
res.status(status).json({ message: message, data: data });
});
firebaseAdmin.initializeFirebaseApp();
// firebaseAdmin.sendPushNotification('cMmphpoxTjizgqORaNDdcf:APA91bGOaGbIM0OljybaRB40YOJ5hORLY65Fdq1eqVP9kA1HB4H8yrgx1lpB4P5XqO3UQxZP4BcHf-B6sbNl3kViK3Q922XfbkabWjmtvbtrLp3tQKLDMaG_lOgIxV-PAvbOx7oNTW71', "Hey it Works", "yeuppp");
//Associations
//Product has a foreign key product_category_id
Product.belongsTo(ProductCategory, {foreignKey: 'product_category_id'});
ProductCategory.hasMany(Product, {foreignKey: 'product_category_id'});
//Product has a foreign key vendor_id
Product.belongsTo(Vendor, {foreignKey: 'vendor_id'});
Vendor.hasMany(Product, {foreignKey: 'vendor_id'});
//Product Template has a foreign key product_category_id
ProductTemplate.belongsTo(ProductCategory, {foreignKey: 'product_category_id'});
User.hasMany(Order, {foreignKey: 'user_id'});
Order.belongsTo(User, {foreignKey: 'user_id'});
Order.belongsTo(Vendor, {foreignKey: 'vendor_id'});
ProductCategory.belongsTo(VendorType, {foreignKey: 'vendor_type_id'});
Vendor.hasMany(Notification, {foreignKey: 'vendor_id'});
Vendor.hasMany(Complaint,{foreignKey: 'vendor_id'} );
Vendor.hasMany(Coupon,{foreignKey: 'vendor_id'} );
sequelize.
//this is to drop and recreate tables
// sync({force: true})
//this is to update some parts of tables
sync({alter: true})
//for normal uses
// sync()
.then(result => {
console.log('Database Connected');
}).catch(err => {
console.log(err);
})
module.exports = app;