add default data at office creation (#86)

This commit is contained in:
Arnaud D. Natali 2023-09-25 20:05:25 +02:00 committed by GitHub
commit c711ced07c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 32 deletions

View File

@ -19,14 +19,6 @@ export default async function main() {
try {
const prisma = new PrismaClient();
const randomString = () => {
const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
let result = "";
for (let i = 10; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
return result;
};
const idNot1 = "dlefvlef";
const idNot2 = "jelkvelknvlkn";
const idNot3 = "rleenrenlnr";
const idNot4 = "ljfeflecnmd";
@ -35,9 +27,9 @@ export default async function main() {
const addresses: Address[] = [
{
address: "148 Avenue du bac",
city: "Rennes",
zip_code: 35000,
address: "template",
city: "template",
zip_code: 0o00,
created_at: new Date(),
updated_at: new Date(),
},
@ -421,9 +413,9 @@ export default async function main() {
const offices: Office[] = [
{
idNot: idNot1,
name: "Office Rennes",
crpcen: randomString(),
idNot: "0000",
name: "Office Template",
crpcen: "0000",
address: addresses[0],
created_at: new Date(),
updated_at: new Date(),

View File

@ -84,9 +84,6 @@ export default class AuthService extends BaseService {
};
}
public generateAccessToken(user: any): string {
console.log("date now", Date.now());
console.log("date local", new Date().toLocaleString())
console.log("date iso",new Date().toISOString());
return jwt.sign({ ...user}, this.variables.ACCESS_TOKEN_SECRET, { expiresIn: "15m" });
}

View File

@ -4,10 +4,13 @@ import { Service } from "typedi";
import jwt from "jsonwebtoken";
import UsersService from "@Services/super-admin/UsersService/UsersService";
import { IdNotJwtPayload } from "../AuthService/AuthService";
import User, { Office, Role } from "le-coffre-resources/dist/SuperAdmin";
import User, { DeedType, Office, OfficeRole, Role, DocumentType } from "le-coffre-resources/dist/SuperAdmin";
import RolesService from "@Services/super-admin/RolesService/RolesService";
import OfficesService from "@Services/super-admin/OfficesService/OfficesService";
import { EOfficeStatus } from "@prisma/client";
import OfficeRolesService from "@Services/super-admin/OfficeRolesService/OfficeRolesService";
import DeedTypesService from "@Services/super-admin/DeedTypesService/DeedTypesService";
import DocumentTypesService from "@Services/super-admin/DocumentTypesService/DocumentTypesService";
interface IIdNotToken {
access_token: string;
@ -93,6 +96,7 @@ enum EIdnotRole {
SUPPLEANT = "Suppléant",
ADMINISTRATEUR = "Administrateur",
RESPONSABLE = "Responsable",
CURATEUR = "Curateur",
}
@Service()
@ -102,6 +106,9 @@ export default class IdNotService extends BaseService {
private userService: UsersService,
private officeService: OfficesService,
private rolesService: RolesService,
private officeRolesService: OfficeRolesService,
private deedTypesService: DeedTypesService,
private documentTypesService: DocumentTypesService,
) {
super();
}
@ -114,10 +121,8 @@ export default class IdNotService extends BaseService {
code: code,
grant_type: "authorization_code",
});
console.log(query)
const token = await fetch(this.variables.IDNOT_BASE_URL + this.variables.IDNOT_CONNEXION_URL + "?" + query, { method: "POST" });
const decodedToken = (await token.json()) as IIdNotToken;
console.log(decodedToken)
const decodedIdToken = jwt.decode(decodedToken.id_token) as IdNotJwtPayload;
return decodedIdToken;
@ -125,29 +130,46 @@ export default class IdNotService extends BaseService {
public async getRole(roleName: string): Promise<Role> {
switch (roleName) {
case EIdnotRole.DIRECTEUR:
return (await this.rolesService.get({ where: { name: "admin" } }))[0]!;
case EIdnotRole.NOTAIRE_TITULAIRE:
return (await this.rolesService.get({ where: { name: "admin" } }))[0]!;
case EIdnotRole.NOTAIRE_ASSOCIE:
return (await this.rolesService.get({ where: { name: "notary" } }))[0]!;
return (await this.rolesService.get({ where: { name: "admin" } }))[0]!;
case EIdnotRole.NOTAIRE_SALARIE:
return (await this.rolesService.get({ where: { name: "notary" } }))[0]!;
case EIdnotRole.COLLABORATEUR:
return (await this.rolesService.get({ where: { name: "collaborator" } }))[0]!;
case EIdnotRole.SECRETAIRE_GENERAL:
return (await this.rolesService.get({ where: { name: "notary" } }))[0]!;
case EIdnotRole.SUPPLEANT:
return (await this.rolesService.get({ where: { name: "notary" } }))[0]!;
case EIdnotRole.ADMINISTRATEUR:
return (await this.rolesService.get({ where: { name: "admin" } }))[0]!;
case EIdnotRole.RESPONSABLE:
return (await this.rolesService.get({ where: { name: "collaborator" } }))[0]!;
return (await this.rolesService.get({ where: { name: "notary" } }))[0]!;
case EIdnotRole.CURATEUR:
return (await this.rolesService.get({ where: { name: "notary" } }))[0]!;
default:
return (await this.rolesService.get({ where: { name: "default" } }))[0]!;
}
}
public async getOfficeRole(roleName: string, officeUid: string) {
switch (roleName) {
case EIdnotRole.NOTAIRE_TITULAIRE:
return (await this.officeRolesService.get({ where: {AND:[{ name: "Notaire" }, {office_uid: officeUid}]}}))[0]!;
case EIdnotRole.NOTAIRE_ASSOCIE:
return (await this.officeRolesService.get({ where: {AND:[{ name: "Notaire" }, {office_uid: officeUid}]}}))[0]!;
case EIdnotRole.NOTAIRE_SALARIE:
return (await this.officeRolesService.get({ where: {AND:[{ name: "Notaire" }, {office_uid: officeUid}]}}))[0]!;
case EIdnotRole.COLLABORATEUR:
return (await this.officeRolesService.get({ where: {AND:[{ name: "Collaborateur" }, {office_uid: officeUid}]}}))[0]!;
case EIdnotRole.SUPPLEANT:
return (await this.officeRolesService.get({ where: {AND:[{ name: "Collaborateur" }, {office_uid: officeUid}]}}))[0]!;
case EIdnotRole.ADMINISTRATEUR:
return (await this.officeRolesService.get({ where: {AND:[{ name: "Collaborateur" }, {office_uid: officeUid}]}}))[0]!;
case EIdnotRole.CURATEUR:
return (await this.officeRolesService.get({ where: {AND:[{ name: "Collaborateur" }, {office_uid: officeUid}]}}))[0]!;
default:
return;
}
}
public getOfficeStatus(statusName: string) {
switch (statusName) {
case "Pourvu":
@ -253,7 +275,6 @@ export default class IdNotService extends BaseService {
public async updateOffice(officeId: string) {
const officeInfos = await this.officeService.getByUid(officeId);
console.log(officeInfos)
const office = Office.hydrate<Office>(officeInfos!);
const searchParams = new URLSearchParams({
key: this.variables.IDNOT_API_KEY,
@ -271,7 +292,6 @@ export default class IdNotService extends BaseService {
return;
}
const officeData = (await officeRawData.json()) as IOfficeData;
console.log(officeData);
let updates = 0;
if(office.name !== officeData.denominationSociale) {
updates++;
@ -342,8 +362,40 @@ export default class IdNotService extends BaseService {
updated_at: null,
},
};
const userToHydrate = User.hydrate<User>(userToAdd);
return await this.userService.create(userToHydrate);
let userHydrated = User.hydrate<User>(userToAdd);
const user = await this.userService.create(userHydrated);
userHydrated = User.hydrate<User>(user);
const officeRoles = await this.officeRolesService.get({ where: { office: { idNot: "0000" }}, include: { office: true, rules: true } });
const documentTypes = await this.documentTypesService.get({ where: { office: { idNot: "0000" }}, include: { office: true } });
const deedTypes = await this.deedTypesService.get({ where:{ office: { idNot: "0000" }}, include: { office: true } });
const officeRolesHydrated = OfficeRole.hydrateArray<OfficeRole>(officeRoles);
const documentTypesHydrated = DocumentType.hydrateArray<DocumentType>(documentTypes);
const deedTypesHydrated = DeedType.hydrateArray<DeedType>(deedTypes);
officeRolesHydrated.forEach(async (officeRole) => {
officeRole.office.uid = user.office_uid;
await this.officeRolesService.create(officeRole);
});
documentTypesHydrated.forEach(async (documentType) => {
documentType.office!.uid = user.office_uid;
await this.documentTypesService.create(documentType);
});
deedTypesHydrated.forEach(async (deedType) => {
deedType.office!.uid = user.office_uid;
await this.deedTypesService.create(deedType);
});
const officeRole = await this.getOfficeRole(userData.typeLien.name, user.office_uid);
if(officeRole) {
const officeRoleHydrated = OfficeRole.hydrate<OfficeRole>(officeRole!);
userHydrated.office_role = officeRoleHydrated;
await this.userService.update(user.uid, userHydrated);
}
return user;
}
return user;
}