const CrudService = require("./CrudService");
const config = require("../config");
const path = require("path");
const RegisterMail = require("../sms-mail/RegisterConsultantMail");
const { v4: uuidv4 } = require("uuid");
const { where } = require("sequelize");
const RegisterAdminMail = require("../sms-mail/RegisterAdminMail");
const Slack = require("../sms-mail/Slack");
/**
* @extends Myintranet.Services.CrudService
* @memberof Myintranet.Services
* @inheritDoc
*/
class RegisterService extends CrudService {
constructor(db) {
super(db.User);
this.db = db;
}
/*
'docs-cv': {
name: 'example.json',
data: <Buffer 7b 0a 20 20 22 73 75 63 63 65 73 73 22 3a 20 74 72 75 65 2c 0a 20 20 22 64 61 74 61 22 3a 20 5b 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 22 69 64 22 3a ... 11654 more bytes>,
size: 11704,
encoding: '7bit',
tempFilePath: '',
truncated: false,
mimetype: 'application/json',
md5: 'bfe45c74c195e3b1690172ae7d378d90',
mv: [Function: mv]
},
'docs-pi': {
name: 'example.json',
data: <Buffer 7b 0a 20 20 22 73 75 63 63 65 73 73 22 3a 20 74 72 75 65 2c 0a 20 20 22 64 61 74 61 22 3a 20 5b 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 22 69 64 22 3a ... 11654 more bytes>,
size: 11704,
encoding: '7bit',
tempFilePath: '',
truncated: false,
mimetype: 'application/json',
md5: 'bfe45c74c195e3b1690172ae7d378d90',
mv: [Function: mv]
},
'docs-jd': {
name: 'example.json',
data: <Buffer 7b 0a 20 20 22 73 75 63 63 65 73 73 22 3a 20 74 72 75 65 2c 0a 20 20 22 64 61 74 61 22 3a 20 5b 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 22 69 64 22 3a ... 11654 more bytes>,
size: 11704,
encoding: '7bit',
tempFilePath: '',
truncated: false,
mimetype: 'application/json',
md5: 'bfe45c74c195e3b1690172ae7d378d90',
mv: [Function: mv]
},
'docs-ass': {
name: 'example.json',
data: <Buffer 7b 0a 20 20 22 73 75 63 63 65 73 73 22 3a 20 74 72 75 65 2c 0a 20 20 22 64 61 74 61 22 3a 20 5b 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 22 69 64 22 3a ... 11654 more bytes>,
size: 11704,
encoding: '7bit',
tempFilePath: '',
truncated: false,
mimetype: 'application/json',
md5: 'bfe45c74c195e3b1690172ae7d378d90',
mv: [Function: mv]
},
'docs-cg': {
name: 'example.json',
data: <Buffer 7b 0a 20 20 22 73 75 63 63 65 73 73 22 3a 20 74 72 75 65 2c 0a 20 20 22 64 61 74 61 22 3a 20 5b 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 22 69 64 22 3a ... 11654 more bytes>,
size: 11704,
encoding: '7bit',
tempFilePath: '',
truncated: false,
mimetype: 'application/json',
md5: 'bfe45c74c195e3b1690172ae7d378d90',
mv: [Function: mv]
},
'docs-ib': {
name: 'example.json',
data: <Buffer 7b 0a 20 20 22 73 75 63 63 65 73 73 22 3a 20 74 72 75 65 2c 0a 20 20 22 64 61 74 61 22 3a 20 5b 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 22 69 64 22 3a ... 11654 more bytes>,
size: 11704,
encoding: '7bit',
tempFilePath: '',
truncated: false,
mimetype: 'application/json',
md5: 'bfe45c74c195e3b1690172ae7d378d90',
mv: [Function: mv]
}
}
*/
getFullDocPathname(doc) {
let slug =
doc.Consultant.id +
"-" +
doc.Consultant.foldername;
let consultantStorageLocation = path.resolve(
path.join(config.storageLocation,slug)
);
switch (doc.typeDoc) {
case "CV":
return path.join(consultantStorageLocation, "CV", doc.name);
case "PI":
return path.join(consultantStorageLocation, "piece_identite", doc.name);
case "JD":
return path.join(
consultantStorageLocation,
"justificatif_domicile",
doc.name
);
case "ASS":
return path.join(
consultantStorageLocation,
"ATT_Securite_social",
doc.name
);
case "SI":
return path.join(
consultantStorageLocation,
"Simulation",
doc.name
);
case "CG":
return path.join(consultantStorageLocation, "Carte_grise", doc.name);
case "IB":
return path.join(
consultantStorageLocation,
"Informations_banquaire",
doc.name
);
case "CM":
return path.join(
consultantStorageLocation,
"Contrat_mutuelle",
doc.name
);
case "PC":
return path.join(
consultantStorageLocation,
"Permis_de_conduire",
doc.name
);
case "MU":
return path.join(
consultantStorageLocation,
"mutuelle",
doc.name
);
case "AP":
return path.join(consultantStorageLocation, "ATT_prevoyance", doc.name);
}
}
async getFullDocPathnam(id) {
let doc = await this.db.ConsultantFiles.findOne({
where: {id},
include: {model: this.db.Consultant, include: this.db.User},
});
let consultantStorageLocation = path.resolve(
config.storageLocation
);
switch (doc.typeDoc) {
case "CV":
return path.join(consultantStorageLocation, "CV", doc.name);
case "PI":
return path.join(consultantStorageLocation, "piece_identite", doc.name);
case "JD":
return path.join(
consultantStorageLocation,
"justificatif_domicile",
doc.name
);
case "ASS":
return path.join(
consultantStorageLocation,
"ATT_Securite_social",
doc.name
);
case "SI":
return path.join(
consultantStorageLocation,
"Simulation",
doc.name
);
case "CG":
return path.join(consultantStorageLocation, "Carte_grise", doc.name);
case "IB":
return path.join(
consultantStorageLocation,
"Informations_banquaire",
doc.name
);
case "CM":
return path.join(
consultantStorageLocation,
"Contrat_mutuelle",
doc.name
);
case "PC":
return path.join(
consultantStorageLocation,
"Permis_de_conduire",
doc.name
);
case "MU":
return path.join(
consultantStorageLocation,
"mutuelle",
doc.name
);
case "AP":
return path.join(consultantStorageLocation, "ATT_prevoyance", doc.name);
}
}
async add(request) {
let { db } = this;
let { body, files } = request;
let client = {
siret: body['client-siret'],
president: body['client-president'],
contact: body['client-contact'],
contactPhone: body['client-contactPhone'],
contactMail: body['client-contactMail'],
address: body['client-address'],
zipCode: body['client-zipcode'],
city: body['client-city'],
rcs: body['client-rcs'],
tvaIntracom: body['client-tvaIntracom'],
denominationSocial: body['client-denominationSocial'],
}
let mission = {
title: body['mission-title'],
dateStart: body['mission-dateStart'],
tjm: body['mission-tjm'],
}
let consultant = {
address: body['infos-address'],
zipCode: body['infos-zipcode'],
city: body['infos-city'],
secuNum: body['infos-secu'],
email: body['infos-email'],
birthDate: body['infos-dateNaissance'],
birthPlace:body['infos-birthPlace'],
codePlace:body['infos-codePlace'],
status: "SUBSCRIBED",
Utilisateur: {
"login": body['infos-email'],
firstname: body['infos-firstname'],
lastname: body['infos-lastname'],
phone: body['infos-phone'],
gender: body['infos-gender']
}
}
const t = await db.sequelize.transaction();
try {
let employeur = await db.Employeur.findOne({ where: { slug: request.params.slug } });
let admins = await employeur.getUtilisateurs(
{
where: {
type: "Admin"
}
}
);
// consultant.employeurId = employeurId;
consultant.type = "Consultant";
consultant.foldername = `${consultant.Utilisateur.firstname}-${consultant.Utilisateur.lastname}`;
let userConsultant = await db.Consultant.create(consultant, {
transaction: t,
include: [
{
model: db.User,
},
],
});
let AdminsEmail = admins.map(a => {
return a.login;
})
// console.log(AdminsEmail.join(","))
await userConsultant.addEmployeur(employeur, { transaction: t });
if (AdminsEmail.length === 0) {
AdminsEmail.push(config.defaultAdminEmail);
}
let Client = await db.Client.create(client, { transaction: t });
mission.ConsultantId = userConsultant.id;
mission.ClientId = Client.id;
mission.EmployeurId = employeur.id;
let Mission = await db.Mission.create(mission, { transaction: t });
let dbFiles = []
for (const key in files) {
if (Object.hasOwnProperty.call(files, key)) {
const file = files[key];
file.ConsultantId = userConsultant.id;
file.typeDoc = key.toUpperCase().replace("DOCS-", "");
let fileName;
fileName = this.getFileName(file.typeDoc, userConsultant.Utilisateur.firstname,userConsultant.Utilisateur.lastname, file.name.split('.').pop());
file.name = fileName;
let dbFile = await db.ConsultantFiles.create(file, { transaction: t });
dbFile.Consultant = userConsultant;
dbFiles.push(dbFile);
await file.mv(this.getFullDocPathname(dbFile));
}
}
//console.log(userConsultant)
let link = await db.SecuredLink.create({
token: uuidv4(),
UtilisateurId: userConsultant.Utilisateur.id,
reason: "Confirmation d'inscription",
}, { transaction: t });
let registermailconsultant = new RegisterMail(userConsultant.Utilisateur, "Confirmation d'inscription - My Intranet", undefined, undefined, employeur, db, undefined, undefined);
let infos = await registermailconsultant.send();
console.log("email Consultant sent");
// (user, subject = "Bienvenue à My-intranet", mission, consultant, employeur, db, client, prevOrCra, emailList)
let registerAdminMail = new RegisterAdminMail(userConsultant.Utilisateur, "Notification d'inscription - My Intranet", undefined, undefined, employeur, db, undefined, undefined, AdminsEmail.join(","));
let infos2 = await registerAdminMail.send();
console.log("emails Admins sent");
//############################### SLACK
let s = new Slack(userConsultant.Utilisateur, dbFiles, userConsultant,Mission, request)
await s.send();
return await t.commit();
} catch (e) {
await t.rollback();
throw e;
}
}
getFileName(typeDoc, firstname, lastname, extension) {
const typeDocNames = {
"CV": "7.CV",
"JD": "2.Justificatif de domicile",
"ASS": "1.Attestation De Droits SécuSocial",
"PI": "4.CNI",
"CG": "5.Carte grise",
"PC": "6.Permis de conduire",
"IB": "3.RIB",
"SI": "8.Simulation",
"CM": "9.Contrat mutuelle",
// "MU": "10.Mutuelle",
"AP": "10.Notice de prévoyance"
};
let baseName = typeDocNames[typeDoc] || typeDoc;
return `${baseName}-${firstname} ${lastname}.${extension}`;
}
}
module.exports = RegisterService;