Newer
Older
/* eslint-disable @typescript-eslint/no-var-requires */
// eslint-disable-next-line @typescript-eslint/no-var-requires
const mongoose = require('mongoose');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const userData = require('./data/users');
const structuresTypeData = require('./data/structuresType');
const categoriesOthersData = require('./data/categoriesOthers');
const categoriesAccompanementsData = require('./data/categoriesAccompanements');
const categoriesFormationData = require('./data/categoriesFormation');
const structuresData = require('./data/structures');
const jobsData = require('./data/jobs');
const employersData = require('./data/employers');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const bcrypt = require('bcrypt');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path');
// eslint-disable-next-line @typescript-eslint/no-var-requires
// const app = require(path.resolve(__dirname, './server'));
require('dotenv').config({ path: path.resolve(__dirname, '../.env') });
/* connect to the database */
const param =
'mongodb://' +
process.env.MONGO_NON_ROOT_USERNAME +
':' +
process.env.MONGO_NON_ROOT_PASSWORD +
'@' +
process.env.MONGO_DB_HOST_AND_PORT +
'/ram';
mongoose.connect(param, { useNewUrlParser: true, useUnifiedTopology: true }).catch((error) => console.log(error));
// Make Mongoose use `findOneAndUpdate()`. Note that this option is `true`
// by default, you need to set it to false.
mongoose.set('useFindAndModify', false);
function hashPassword() {
return bcrypt.hashSync(process.env.USER_PWD, process.env.SALT);
}
const handleError = async (name, err) => {
/* show messages */
if (err) {
if (err.code === 26) console.error(`-- ${name} collection does not exists --`);
else throw err;
} else console.log(`-- ${name} collection dropped --`);
};
const usersSchema = mongoose.Schema({
name: String,
surname: String,
email: String,
emailVerified: Boolean,
validationToken: String,
resetPasswordToken: String,
role: Number,
changeEmailToken: String,
newEmail: String,
structuresLink: [],
structureOutdatedMailSent: [],
pendingStructuresLink: [],
password: String,
phone: String,
employer: String,
job: String,
const jobsSchema = mongoose.Schema(
{
name: String,
hasPersonalOffer: Boolean,
validated: Boolean,
},
{ collection: 'jobs' }
);
const employersSchema = mongoose.Schema(
{
name: String,
validated: Boolean,
},
{ collection: 'employers' }
);
const structuresTypeSchema = mongoose.Schema(
{
name: String,
values: [],
},
{ collection: 'structuretype' }
);
const categoriesOthersSchema = mongoose.Schema({
name: String,
id: String,
modules: [],
});
const categoriesAccompanementsSchema = mongoose.Schema({
name: String,
id: String,
modules: [],
});
const categoriesFormationSchema = mongoose.Schema({
name: String,
id: String,
modules: [],
});
const structuresSchema = mongoose.Schema({
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
numero: String,
createdAt: String,
updatedAt: String,
structureName: String,
structureType: String,
description: String,
lockdownActivity: String,
address: Object,
contactPhone: String,
contactMail: String,
website: String,
facebook: String,
twitter: String,
instagram: String,
linkedin: String,
pmrAccess: Boolean,
accessModality: [],
otherDescription: String,
labelsQualifications: [],
publics: [],
exceptionalClosures: String,
publicsAccompaniment: [],
proceduresAccompaniment: [],
remoteAccompaniment: Boolean,
baseSkills: [],
accessRight: [],
socialAndProfessional: [],
parentingHelp: [],
digitalCultureSecurity: [],
equipmentsAndServices: [],
freeWorkShop: Boolean,
nbComputers: Number,
nbPrinters: Number,
nbTablets: Number,
nbNumericTerminal: Number,
nbScanners: Number,
hours: Object,
coord: [],
deletedAt: Date,
accountVerified: Boolean,
});
const user = mongoose.model('users', usersSchema);
const structuresType = mongoose.model('structureType', structuresTypeSchema);
const categoriesOthers = mongoose.model('categoriesOthers', categoriesOthersSchema);
const categoriesAccompanements = mongoose.model('CategoriesAccompagnement', categoriesAccompanementsSchema);
const categoriesFormation = mongoose.model('categoriesFormation', categoriesFormationSchema);
const structures = mongoose.model('structures', structuresSchema);
const jobs = mongoose.model('jobs', jobsSchema);
const employers = mongoose.model('employers', employersSchema);
mongoose.connection.dropCollection('users', async (err) => {
await handleError('Users', err);
// Init passsword
console.log('-- Users password encryption based on .env --');
userData.data.forEach((user) => {
user.password = hashPassword();
});
// save model to database
user.create(userData.data, (error) => {
console.log('-- Users collection initialized --');
});
});
/* Create structures ref */
mongoose.connection.dropCollection('structuretype', async (err) => {
await handleError('structureType', err);
structuresType.create(structuresTypeData.data, (error) => {
if (error) return console.error(error);
});
});
mongoose.connection.dropCollection('categoriesothers', async (err) => {
await handleError('categoriesOthers', err);
categoriesOthers.create(categoriesOthersData.data, (error) => {
if (error) return console.error(error);
});
});
mongoose.connection.dropCollection('categoriesaccompagnements', async (err) => {
await handleError('categoriesAccompanements', err);
categoriesAccompanements.create(categoriesAccompanementsData.data, (error) => {
if (error) return console.error(error);
});
});
mongoose.connection.dropCollection('categoriesformations', async (err) => {
await handleError('categoriesFormations', err);
categoriesFormation.create(categoriesFormationData.data, (error) => {
if (error) return console.error(error);
});
});
/* Create structures */
mongoose.connection.dropCollection('structures', async (err) => {
await handleError('structures', err);
structures.create(structuresData.data, (error) => {
if (error) return console.error(error);
});
});
/* Create structures */
mongoose.connection.dropCollection('jobs', async (err) => {
await handleError('jobs', err);
jobs.create(jobsData.data, (error) => {
if (error) return console.error(error);
});
});
/* Create structures */
mongoose.connection.dropCollection('employers', async (err) => {
await handleError('employers', err);
employers.create(employersData.data, (error) => {
if (error) return console.error(error);