forked from dev-bhavya/nitj-website-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
128 lines (114 loc) · 3.1 KB
/
utils.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
125
126
127
128
const mongoose = require('mongoose');
// Description: contains functions that are used in multiple files
const sendError = (res, err) => {
// used to send error to client and console
console.log(err);
res.status(400).json(err);
};
const validateID = (id) => {
// used to validate id
const isValidId = mongoose.Types.ObjectId.isValid(id);
if (!isValidId) {
// return the error as string to be used in catch
return Promise.reject('Invalid ID, must be 12 bytes or a string of 24 hex characters');
}
return Promise.resolve();
};
// logUpdates
const regexForUpdateLogs = '/^findOneAnd/';
async function logUpdates(next) {
// Check updated fields
const updatedFields = Object.keys(this._update["$set"]);
// Compare fields with existing document in database
const existingDoc = await this.model.findOne(this.getQuery());
const changedFields = updatedFields.filter((field) => {
return existingDoc[`${field}`] !== this._update["$set"][`${field}`];
});
// Remove unnecessary fields from changed fields
const unnecessaryFields = ["updatedAt", "updateLogs", "_id", "__v", "order"];
const filteredChangedFields = changedFields.filter((field) => {
return !unnecessaryFields.includes(field);
});
const updateLogsPrevious = existingDoc.updateLogs;
const updateLogsNew = `${new Date().toLocaleString()} - ${filteredChangedFields.join(" ")}`;
// Update the document's updateLogs field
this._update["$set"].updateLogs = updateLogsPrevious
? `${updateLogsPrevious}\n${updateLogsNew}`
: updateLogsNew;
// Call the next middleware in the chain
next();
}
const commonFieldsForAll = {
visible: {
type: Boolean,
default: true,
required: true,
},
sourceOfInfo: {
type: {
name: {
type: String,
default: null
},
department: {
type: String,
default: null
},
designation: {
type: String,
default: null
},
email: {
type: String,
default: null
},
}
},
order: {
type: Number,
default: 0
},
visibilityChangedAt: {
type: Date,
default: null
},
updateLogs: {
type: Array,
default: []
},
};
const fields = {
webURL: {
link: {
type: String,
default: ""
},
newPage: {
type: Boolean,
default: false,
},
},
newGIF: {
new: {
type: Boolean,
default: true
},
},
admissionsFields: {
degree: {
type: String,
required: true,
notEmpty: true,
enum: ['BTECH', 'MTECH-CCMT', 'MTECH-SELF', 'MSC', 'MBA', 'PHD', 'FOREIGN'],
},
}
};
// exports these functions to be used in other files
module.exports = {
sendError,
validateID,
commonFieldsForAll,
logUpdates,
regexForUpdateLogs,
fields,
};