Newer
Older
/* eslint-disable @typescript-eslint/no-var-requires */
const mongoose = require('mongoose');
const userData = require('./data/users');
const structuresTypeData = require('./data/structuresType');
const categoriesData = require('./data/categoriesData');
const structuresData = require('./data/structures');
const jobsData = require('./data/jobs');
const employersData = require('./data/employers');
const path = require('path');
// 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.
function hashPassword() {
return bcrypt.hashSync(process.env.USER_PWD, process.env.SALT);
}
// define Schema
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: [],
unattachedSince: Date,
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 categoriesSchema = mongoose.Schema({
theme: String,
const structuresSchema = mongoose.Schema({
createdAt: String,
updatedAt: String,
structureName: String,
structureType: String,
description: String,
address: Object,
contactPhone: String,
contactMail: String,
website: String,
facebook: String,
twitter: String,
instagram: String,
linkedin: String,
pmrAccess: Boolean,
otherDescription: String,
exceptionalClosures: String,
categories: {},
freeWorkShop: Boolean,
nbComputers: Number,
nbPrinters: Number,
nbTablets: Number,
nbNumericTerminal: Number,
nbScanners: Number,
hours: Object,
coord: [],
deletedAt: Date,
accountVerified: Boolean,
lastUpdateMail: Date,
const user = mongoose.model('users', usersSchema);
const structuresType = mongoose.model('structuretype', structuresTypeSchema);
const categories = mongoose.model('categories', categoriesSchema);
const structures = mongoose.model('structures', structuresSchema);
const jobs = mongoose.model('jobs', jobsSchema);
const employers = mongoose.model('employers', employersSchema);
async function deleteData() {
let usersPromise = mongoose.connection.dropCollection('users');
let structureTypePromise = mongoose.connection.dropCollection('structuretype');
let structuresPromise = mongoose.connection.dropCollection('structures');
let jobPromise = mongoose.connection.dropCollection('jobs');
/* Drop jobs groups*/
let jobGroupPromise = mongoose.connection.dropCollection('jobsgroups');
/* Drop employers */
let employersPromise = mongoose.connection.dropCollection('employers');
let categoriesPromise = mongoose.connection.dropCollection('categories');
await Promise.all([
employersPromise,
structureTypePromise,
jobPromise,
structuresPromise,
usersPromise,
categoriesPromise,
]);
return true;
}
async function createData() {
let employersPromise = employers.create(employersData.data);
let structureTypePromise = structuresType.create(structuresTypeData.data);
let structuresPromise = structures.create(structuresData.data);
let jobPromise = jobs.create(jobsData.data);
let categoriesPromise = categories.create(categoriesData.data);
// Init password
console.log('-- Users password encryption based on .env --');
const hashedPassword = hashPassword();
user.password = hashedPassword;
let userPromise = user.create(userData.data);
await Promise.all([
employersPromise,
structureTypePromise,
jobPromise,
structuresPromise,
userPromise,
categoriesPromise,
]);
return true;
}
function main() {
deleteData()
.then(() => {
console.log('-- Deleting data done --');
})
.catch((err) => {
if (err.code === 26) console.warn(`-- collection(s) does not exists --`);
else console.error(err);
})
.then(() => {
createData()
.then(() => {
console.log('-- Init db done--');
process.exit(0);
})
.catch((err) => {
console.error(err);
});
});
}