diff --git a/.circleci/config.yml b/.circleci/config.yml index 2c8347dd..6f2d2815 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -20,7 +20,7 @@ jobs: - setup_remote_docker: version: 20.10.12 docker_layer_caching: true - - run: docker login rg.fr-par.scw.cloud/lecoffre -u nologin -p $SCW_SECRET_KEY + - run: docker login rg.fr-par.scw.cloud/lecoffre -u nologin -p $SCW_SECRET_KEY_BIS - run: docker build --tag rg.fr-par.scw.cloud/lecoffre/back:${CIRCLE_SHA1:0:7} . - run: docker push rg.fr-par.scw.cloud/lecoffre/back:${CIRCLE_SHA1:0:7} @@ -33,7 +33,7 @@ jobs: parameters: env: type: string - default: stg + default: ppd steps: - checkout - kubernetes/install-kubeconfig: diff --git a/devops/ppd.values.yaml b/devops/ppd.values.yaml index dce7e33f..3300e6cf 100644 --- a/devops/ppd.values.yaml +++ b/devops/ppd.values.yaml @@ -1,6 +1,6 @@ dockerPullSecret: docker-pull-secret -scwSecretKey: AgCgjF5QEzxT3GYTS5B6cmQ0e+0/qFWzKaUDSi+Vjc7RoameuvaIJvTXMBkS3he1oy1ulbB34v6vpZI2kxnGNqERA/U5BaYDAyfKSBwMAy4br7HVKhhuwkoF5qoG5JzJXseSmqB1U9vncVIGOZWzJc1Y4/eGlWcvLcLyfw2z/WEpyeNiWJfEhTYpJOB7gv0XnRb2U/JM3jRy1QgEUIk1WR6kgBalF+xaczPQ6uKh+PR2pqkbZa3WaKUrddmzNsgEz4d8PZMWt8IBwR2JOQEHUqCd34p/pJNyLdUgcdDhg02DKwn1oRoAxKTbAio/a7WrMbodjCb3TNWIYGal5mFmItZ7Ok/EBmUf4E85eOkTR+j8ynuuiexld3Q5Kw3o8LsHjgzVL9uP+T2rYaKkjtVt+YQRX1U8l9CrsdUEz0/wEBA0jwCWMfnh1qhD5pM/xwwjsEEAcK4rYV+Q7iAgGZZvZBCQ5aEHzrtn5D95tr1GZCV2hmrW6Seu+LKKLVBS1JmsuEsOuhudYsEK9m2RYVcxbjuS5eokKEjNrGobf2oB8rhBByavfw1JTBixR5JrI8lcYlnCa+oEhxXKJY+4Fx5SAB4YaLCMSo5vw6zsFQ3WKQzlEmCFt+EnapS+a+MGrdlwq07OHTDpvgk/1z39hopoCuhhKckGGfErLXsTYQvDOkFu+EPzgY7m7qDw/d9pSiht5tuSOkAqeOgm7tpNkUufZhaXmP+1aT7i+H5gq1JILGAmXzTI5Wc= +scwSecretKey: AgChoEnPitXp4Ny/rVMEcevaWKNVpyj2cJYAcq+yFqKwVwnLB+ffDvwqz9XBHu+6d4Nyyjkf37zUAMoaM21lEDWA7x3zfG2/D/j+rvX1qxzZgLD0mjBk7fGElVm332I6JA83oInes8AMMYEDPLElzHnpKRb9KtkIP4NzgOcCeW0ijft3N7Vroez6LEHsBPCA1I9XjKSkGEDvrO0MhWX3iJOlfz+SPMfJAV7rPawOs0ZmohTHrPW8qIvGDn8HCzKyU8zRBoMt+Ogpf5pH4U3JryEFuqD61KAQgablAM8edPIvsgNno9HAEuC2QtRLYA9aUhuKdaKuS58c9P2E80PHWXIlbpFCg6EugQTgNfnYp+3qDUNz8edeCfapYLvF4s9eCMGyMsGnpDR8EDNOyuGy7Y3l7okX8Xqu464gMp9E+hX7bHkcD6a4xfyIgJcWxsku0tm1TH1dpn4M1UXRuyZZif8P08nuE6MTUL67sAR9J1lpn4lVEL4kflk0pP2tZ5ncgPQFafJrRz05krMb0eU5tb2H4gs7ao/LL6idWo8MM9K1yr8lIuT5x2WW5CX+RjA+i50ex114V6vX3PNP5oVyt+DynTUB9QmXzVm3oLfDc3Cae1uqh7X0CFd+xiztJBtg0VtJaD/xUJcuWfY4cV2lERo9fRrykltzlJqiXHO4nowt8OtN0BcViVV8NJhPhYFzyb4ympxpOlTjm3GETuT2TYhUqdgS9nzleEAbOmOHZdIO2COunPE= lecoffreBack: serviceAccountName: lecoffre-back-sa @@ -30,7 +30,7 @@ lecoffreBack: nginx.ingress.kubernetes.io/force-ssl-redirect: "true" env: - key: .env - scwID: "id:430001f8-68ab-47b2-92e8-38024c35a80d" + scwID: "id:2be9510b-bb1f-4fbe-ab3e-3dc11fb49051" lecoffreCron: serviceAccountName: lecoffre-cron-sa @@ -50,5 +50,5 @@ lecoffreCron: # key is name of the environment variable, scwID is the secret ID in SCW with "id:" in front env: - key: .env - scwID: "id:430001f8-68ab-47b2-92e8-38024c35a80d" + scwID: "id:2be9510b-bb1f-4fbe-ab3e-3dc11fb49051" diff --git a/devops/stg.values.yaml b/devops/stg.values.yaml index 9f119e2f..9b2cf79c 100644 --- a/devops/stg.values.yaml +++ b/devops/stg.values.yaml @@ -1,6 +1,6 @@ dockerPullSecret: docker-pull-secret -scwSecretKey: AgChoEnPitXp4Ny/rVMEcevaWKNVpyj2cJYAcq+yFqKwVwnLB+ffDvwqz9XBHu+6d4Nyyjkf37zUAMoaM21lEDWA7x3zfG2/D/j+rvX1qxzZgLD0mjBk7fGElVm332I6JA83oInes8AMMYEDPLElzHnpKRb9KtkIP4NzgOcCeW0ijft3N7Vroez6LEHsBPCA1I9XjKSkGEDvrO0MhWX3iJOlfz+SPMfJAV7rPawOs0ZmohTHrPW8qIvGDn8HCzKyU8zRBoMt+Ogpf5pH4U3JryEFuqD61KAQgablAM8edPIvsgNno9HAEuC2QtRLYA9aUhuKdaKuS58c9P2E80PHWXIlbpFCg6EugQTgNfnYp+3qDUNz8edeCfapYLvF4s9eCMGyMsGnpDR8EDNOyuGy7Y3l7okX8Xqu464gMp9E+hX7bHkcD6a4xfyIgJcWxsku0tm1TH1dpn4M1UXRuyZZif8P08nuE6MTUL67sAR9J1lpn4lVEL4kflk0pP2tZ5ncgPQFafJrRz05krMb0eU5tb2H4gs7ao/LL6idWo8MM9K1yr8lIuT5x2WW5CX+RjA+i50ex114V6vX3PNP5oVyt+DynTUB9QmXzVm3oLfDc3Cae1uqh7X0CFd+xiztJBtg0VtJaD/xUJcuWfY4cV2lERo9fRrykltzlJqiXHO4nowt8OtN0BcViVV8NJhPhYFzyb4ympxpOlTjm3GETuT2TYhUqdgS9nzleEAbOmOHZdIO2COunPE= +scwSecretKey: 59bcf27d-bee3-4d14-8b4d-03fd6a8be6cd lecoffreBack: serviceAccountName: lecoffre-back-sa @@ -51,6 +51,4 @@ lecoffreCron: # key is name of the environment variable, scwID is the secret ID in SCW with "id:" in front env: - key: .env - scwID: "id:2be9510b-bb1f-4fbe-ab3e-3dc11fb49051" - - + scwID: "id:2be9510b-bb1f-4fbe-ab3e-3dc11fb49051" \ No newline at end of file diff --git a/devops/templates/service-account.yaml b/devops/templates/service-account.yaml index dbe9055d..7796d3cb 100644 --- a/devops/templates/service-account.yaml +++ b/devops/templates/service-account.yaml @@ -1,7 +1,7 @@ apiVersion: v1 kind: ServiceAccount metadata: - name: {{ .Values.lecoffreBack.serviceAccountName }} + name: {{ .Values.lecoffreBack.serviceAccountName }} --- @@ -10,14 +10,14 @@ kind: Secret metadata: name: {{ .Values.lecoffreBack.serviceAccountName }}-token annotations: - kubernetes.io/service-account.name: {{ .Values.lecoffreBack.serviceAccountName }} + kubernetes.io/service-account.name: {{ .Values.lecoffreBack.serviceAccountName }} type: kubernetes.io/service-account-token --- apiVersion: v1 kind: ServiceAccount metadata: - name: {{ .Values.lecoffreCron.serviceAccountName }} + name: {{ .Values.lecoffreCron.serviceAccountName }} --- diff --git a/devops/values.yaml b/devops/values.yaml index 0449dec5..a5538cbf 100644 --- a/devops/values.yaml +++ b/devops/values.yaml @@ -19,10 +19,10 @@ lecoffreBack: limits: memory: 2Gi ingress: - host: api.stg.lecoffre.smart-chain.fr + host: api.ppd.lecoffre.smart-chain.fr tls: hosts: - - api.stg.lecoffre.smart-chain.fr + - api.ppd.lecoffre.smart-chain.fr secretName: api-tls annotations: kubernetes.io/ingress.class: nginx diff --git a/src/app/HomeController.ts b/src/app/HomeController.ts index 2d84c394..1f19e512 100644 --- a/src/app/HomeController.ts +++ b/src/app/HomeController.ts @@ -6,6 +6,7 @@ import ApiController from "@Common/system/controller-pattern/ApiController"; @Controller() @Service() export default class HomeController extends ApiController { + @Get("/") protected async get(req: Request, res: Response) { // const query = processFindManyQuery(req.query); diff --git a/src/app/api/customer/DocumentsController.ts b/src/app/api/customer/DocumentsController.ts index 42fdd63b..d802ba36 100644 --- a/src/app/api/customer/DocumentsController.ts +++ b/src/app/api/customer/DocumentsController.ts @@ -92,9 +92,7 @@ export default class DocumentsController extends ApiController { protected async post(req: Request, response: Response) { try { //init Document resource with request body values - const documentEntity = Document.hydrate(req.body); - console.log(documentEntity); - + const documentEntity = Document.hydrate(req.body); //validate document await validateOrReject(documentEntity, { groups: ["createDocument"], forbidUnknownValues: false }); diff --git a/src/app/api/customer/FilesController.ts b/src/app/api/customer/FilesController.ts index 83f595ec..4be714ef 100644 --- a/src/app/api/customer/FilesController.ts +++ b/src/app/api/customer/FilesController.ts @@ -90,8 +90,7 @@ export default class FilesController extends ApiController { //init File resource with request body values const fileEntity = File.hydrate(JSON.parse(req.body["q"])); - console.log(fileEntity); - + //validate File // await validateOrReject(fileEntity, { groups: ["createFile"] }); diff --git a/src/app/api/idnot/UserController.ts b/src/app/api/idnot/UserController.ts index e15b0c47..8540aa56 100644 --- a/src/app/api/idnot/UserController.ts +++ b/src/app/api/idnot/UserController.ts @@ -7,7 +7,6 @@ import { JwtPayload } from "jsonwebtoken"; import IdNotService from "@Services/common/IdNotService/IdNotService"; - @Controller() @Service() export default class UserController extends ApiController { @@ -29,14 +28,18 @@ export default class UserController extends ApiController { const idNotToken = await this.idNotService.getIdNotToken(code); const user = await this.idNotService.getOrCreateUser(idNotToken); + if(!user) { + this.httpUnauthorized(response); + return; + } await this.idNotService.updateUser(user.uid); await this.idNotService.updateOffice(user.office_uid); const payload = await this.authService.getUserJwtPayload(user.idNot); const accessToken = this.authService.generateAccessToken(payload); const refreshToken = this.authService.generateRefreshToken(payload); - - this.httpSuccess(response, {accessToken, refreshToken}); + + this.httpSuccess(response, { accessToken, refreshToken }); } catch (error) { console.log(error); this.httpInternalError(response); @@ -77,7 +80,7 @@ export default class UserController extends ApiController { let accessToken; this.authService.verifyRefreshToken(token, (err, userPayload) => { if (err) { - console.log(err) + console.log(err); this.httpUnauthorized(response); return; } @@ -89,7 +92,7 @@ export default class UserController extends ApiController { }); //success - this.httpSuccess(response, {accessToken}); + this.httpSuccess(response, { accessToken }); } catch (error) { this.httpInternalError(response); return; diff --git a/src/app/index.ts b/src/app/index.ts index fa011b2c..276d556f 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -48,10 +48,12 @@ import VotesController from "./api/super-admin/VotesController"; import LiveVoteController from "./api/super-admin/LiveVoteController"; + /** * @description This allow to declare all controllers used in the application */ export default { + start: () => { Container.get(HomeController); Container.get(UsersControllerSuperAdmin); diff --git a/src/common/config/variables/Variables.ts b/src/common/config/variables/Variables.ts index 14e5e0a6..ef3333f8 100644 --- a/src/common/config/variables/Variables.ts +++ b/src/common/config/variables/Variables.ts @@ -25,6 +25,9 @@ export class BackendVariables { @IsNotEmpty() public readonly API_ROOT_URL!: string; + @IsNotEmpty() + public readonly APP_HOST!: string; + @IsOptional() public readonly APP_LABEL!: string; @@ -91,6 +94,7 @@ export class BackendVariables { this.DATABASE_NAME = process.env["DATABASE_NAME"]!; this.DATABASE_URL = process.env["DEV_PRISMA_STUDIO_DB_URL"]!; this.API_ROOT_URL = process.env["API_ROOT_URL"]!; + this.APP_HOST = process.env["APP_HOST"]!; this.APP_PORT = process.env["APP_PORT"]!; this.APP_ROOT_URL = process.env["APP_ROOT_URL"]!; this.APP_LABEL = process.env["APP_LABEL"]!; @@ -117,7 +121,7 @@ export class BackendVariables { try { await validateOrReject(this, validationOptions); } catch (error: any) { - if (process.env["ENV"] === "dev") { + if (process.env["ENV"] === "dev" || process.env["ENV"] === 'stg') { throw error; } throw new Error("Some env variables are required!"); diff --git a/src/common/databases/seeders/seeder.ts b/src/common/databases/seeders/seeder.ts index 89b9aebe..773a3c97 100644 --- a/src/common/databases/seeders/seeder.ts +++ b/src/common/databases/seeders/seeder.ts @@ -669,6 +669,18 @@ export default async function main() { created_at: new Date(), updated_at: new Date(), }, + { + name: "POST anchors", + label: "Ancrer un dossier", + created_at: new Date(), + updated_at: new Date(), + }, + { + name: "GET anchors", + label: "Vérifier l'ancrage un dossier", + created_at: new Date(), + updated_at: new Date(), + }, { name: "POST deed-types", label: "Création des types d'actes", @@ -756,14 +768,14 @@ export default async function main() { label: "Administrateur", created_at: new Date(), updated_at: new Date(), - rules: rules.slice(0, 33), + rules: rules.slice(0, 35), }, { name: "notary", label: "Notaire", created_at: new Date(), updated_at: new Date(), - rules: rules.slice(0, 23), + rules: rules.slice(0, 25), }, { name: "default", @@ -780,7 +792,7 @@ export default async function main() { created_at: new Date(), updated_at: new Date(), office: offices[0]!, - rules: rules.slice(0, 33), + rules: rules.slice(0, 35), }, { name: "Collaborateur", diff --git a/src/common/emails/EmailBuilder.ts b/src/common/emails/EmailBuilder.ts index b66c8238..5c77409b 100644 --- a/src/common/emails/EmailBuilder.ts +++ b/src/common/emails/EmailBuilder.ts @@ -45,6 +45,34 @@ export default class EmailBuilder { nbTrySend: null, lastTrySendDate: null, }); - + } + + public async sendRecapEmails(usersToEmail: [{email: string, civility: string, last_name: string}]){ + usersToEmail.forEach(user => { + const to = user.email; + + const templateVariables = { + civility: user.civility, + last_name: user.last_name, + link: "http://localhost:3000" + }; + + const templateName = ETemplates.DOCUMENT_RECAP; + const subject = "Récapitulatif hebdromadaire"; + + this.mailchimpService.create({ + templateName, + to, + subject, + templateVariables, + uid: "", + from: null, + cc: [], + cci: [], + sentAt: null, + nbTrySend: null, + lastTrySendDate: null, + }); + }); } } diff --git a/src/common/emails/Templates/EmailTemplates.ts b/src/common/emails/Templates/EmailTemplates.ts index 28f7f6fc..081cfadb 100644 --- a/src/common/emails/Templates/EmailTemplates.ts +++ b/src/common/emails/Templates/EmailTemplates.ts @@ -1,4 +1,5 @@ export const ETemplates = { DOCUMENT_ASKED: "DOCUMENT_ASKED", DOCUMENT_REFUSED: "DOCUMENT_REFUSED", + DOCUMENT_RECAP: "DOCUMENT_RECAP", }; \ No newline at end of file diff --git a/src/common/repositories/DocumentsRepository.ts b/src/common/repositories/DocumentsRepository.ts index 6a86cdd8..aec9b82d 100644 --- a/src/common/repositories/DocumentsRepository.ts +++ b/src/common/repositories/DocumentsRepository.ts @@ -21,6 +21,7 @@ export default class DocumentsRepository extends BaseRepository { * @description : Find many documents */ public async findMany(query: Prisma.DocumentsFindManyArgs) { + query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows); return this.model.findMany(query); } diff --git a/src/entries/Cron.ts b/src/entries/Cron.ts index 0b97bcec..a34cee31 100644 --- a/src/entries/Cron.ts +++ b/src/entries/Cron.ts @@ -1,17 +1,22 @@ import "module-alias/register"; import "reflect-metadata"; + import { Container } from "typedi"; import { BackendVariables } from "@Common/config/variables/Variables"; import CronService from "@Services/common/CronService/CronService"; (async () => { + console.log("Cron started"); + try { const variables = await Container.get(BackendVariables).validate(); Container.get(CronService).archiveFiles(); await Container.get(CronService).updateUsers(); if(variables.ENV !== "dev"){ Container.get(CronService).sendMails(); + Container.get(CronService).sendRecapMails(); } + } catch (e) { console.error(e); } diff --git a/src/services/common/AuthService/AuthService.ts b/src/services/common/AuthService/AuthService.ts index fe8d4ded..26727a52 100644 --- a/src/services/common/AuthService/AuthService.ts +++ b/src/services/common/AuthService/AuthService.ts @@ -84,11 +84,11 @@ export default class AuthService extends BaseService { }; } public generateAccessToken(user: any): string { - return jwt.sign({ ...user }, this.variables.ACCESS_TOKEN_SECRET, { expiresIn: "15m" }); + return jwt.sign({ ...user, iat: Math.floor(Date.now() / 1000)}, this.variables.ACCESS_TOKEN_SECRET, { expiresIn: "15m" }); } public generateRefreshToken(user: any): string { - return jwt.sign({ ...user }, this.variables.REFRESH_TOKEN_SECRET, { expiresIn: "1h" }); + return jwt.sign({ ...user, iat: Math.floor(Date.now() / 1000)}, this.variables.REFRESH_TOKEN_SECRET, { expiresIn: "1h" }); } public verifyAccessToken(token: string, callback?: VerifyCallback) { diff --git a/src/services/common/CronService/CronService.ts b/src/services/common/CronService/CronService.ts index d8c6cd38..ca65ce1d 100644 --- a/src/services/common/CronService/CronService.ts +++ b/src/services/common/CronService/CronService.ts @@ -24,6 +24,21 @@ export default class CronService { } } + public async sendRecapMails() { + const cronJob = new CronJob("0 20 * * FRI", async () => { // Every friday at 20:00 + try { + await this.mailchimpService.sendRecapEmails(); + } catch (e) { + console.error(e); + } + }); + + // Start job + if (!cronJob.running) { + cronJob.start(); + } + } + public async archiveFiles() { const cronJob = new CronJob("0 0 * * MON", async () => { // Every monday at midnight try { @@ -39,7 +54,7 @@ export default class CronService { } } public async updateUsers() { - const cronJob = new CronJob("*/15 * * * *", async () => { // Every 15 minutes + const cronJob = new CronJob("0 0 * * *", async () => { // Once a day at midnight try { await this.idNotService.updateOffices(); await this.idNotService.updateUsers(); diff --git a/src/services/common/IdNotService/IdNotService.ts b/src/services/common/IdNotService/IdNotService.ts index 17c3378b..14758203 100644 --- a/src/services/common/IdNotService/IdNotService.ts +++ b/src/services/common/IdNotService/IdNotService.ts @@ -62,6 +62,9 @@ interface IOfficeData { statutEntite: { name: string; }; + typeEntite: { + name: string; + } } interface IOfficeLocation { @@ -107,13 +110,16 @@ export default class IdNotService extends BaseService { const query = new URLSearchParams({ client_id: this.variables.IDNOT_CLIENT_ID, client_secret: this.variables.IDNOT_CLIENT_SECRET, - redirect_uri: `http://0.0.0.0:3000/authorized-client`, + redirect_uri: `${this.variables.APP_HOST}/authorized-client`, 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; } @@ -165,19 +171,44 @@ export default class IdNotService extends BaseService { const searchParams = new URLSearchParams({ key: this.variables.IDNOT_API_KEY, }); - const userRawData = await (await fetch( - `${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/personnes/${user.idNot}/rattachements?` + + let userData = await (await fetch( + `${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/rattachements/${user.idNot}_${user.office_membership!.idNot}?` + searchParams, { method: "GET", }, - )).json() as any; - if (userRawData.totalResultCount === 0) { - await this.userService.updateCheckedAt(user.uid!); - //await this.userService.delete(user.uid!); - return; + )).json() as IRattachementData; + + if (!userData.statutDuRattachement) { + const rattachements = await (await fetch( + `${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/personnes/${user.idNot}/rattachements?` + + searchParams, + { + method: "GET", + }, + )).json() as any; + if (rattachements.totalResultCount === 0) { + await this.userService.updateCheckedAt(user.uid!); + //await this.userService.delete(user.uid!); + return; + } + const rattachementsResults = rattachements.result as IRattachementData[]; + if(!rattachementsResults) return; + rattachementsResults.forEach(async (rattachement) => { + if (rattachement.statutDuRattachement) { + const officeData = await (await fetch( + `${this.variables.IDNOT_API_BASE_URL + rattachement.entiteUrl}?` + + searchParams, + { + method: "GET", + }, + )).json() as IOfficeData; + if(officeData.typeEntite.name === "office") { + userData = rattachement; + } + } + }); } - const userData = userRawData.result[0] as IRattachementData; const roleFromIdNot = await this.getRole(userData.typeLien.name); let updates = 0; @@ -186,24 +217,22 @@ export default class IdNotService extends BaseService { user.role = roleFromIdNot; } - if (user.office_membership!.idNot !== userData.entiteUrl.split("/")[5]!) { + if (user.office_membership!.idNot !== userData.entite.ou) { updates++; - let officeData = (await this.officeService.get({ where: { idNot: userData.entiteUrl.split("/")[5]! } }))[0]; + let officeData = (await this.officeService.get({ where: { idNot:userData.entite.ou } }))[0]; if (!officeData) { - const officeIdNotData = (await ( - await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entiteUrl}?` + searchParams, { method: "GET" }) - ).json()) as IOfficeData; + const officeLocationData = (await ( + await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entite.locationsUrl}?` + searchParams, { method: "GET" }) + ).json()) as IOfficeLocation; const office = { - idNot: userData.entiteUrl.split("/")[5]!, - name: officeIdNotData.denominationSociale - ? officeIdNotData.denominationSociale - : `office ${userData.entiteUrl.split("/")[5]!}`, - crpcen: officeIdNotData.codeCrpcen, - office_status: this.getOfficeStatus(officeIdNotData.statutEntite.name), + idNot: userData.entite.ou, + name: userData.entite.denominationSociale, + crpcen: userData.entite.codeCrpcen, + office_status: this.getOfficeStatus(userData.entite.statutEntite.name), address: { - address: officeIdNotData.departementResidence[0]!.libelle, //officeLocationData.result[0]!.adrPostale4, - city: "city", //officeLocationData.result[0]!.adrPostaleVille, - zip_code: Number(officeIdNotData.departementResidence[0]!.code), + address: officeLocationData.result[0]!.adrGeo4, + city: officeLocationData.result[0]!.adrGeoVille.split(" ")[0] ?? officeLocationData.result[0]!.adrGeoVille, + zip_code: Number(officeLocationData.result[0]!.adrGeoCodePostal), created_at: null, updated_at: null, }, @@ -229,6 +258,7 @@ 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(officeInfos!); const searchParams = new URLSearchParams({ key: this.variables.IDNOT_API_KEY, @@ -246,6 +276,7 @@ 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++; @@ -272,10 +303,19 @@ export default class IdNotService extends BaseService { }) ).json()) as IRattachementData; + + if(!userData.statutDuRattachement || userData.entite.typeEntite.name !== "office") { + return null; + } + const officeLocationData = (await ( await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entite.locationsUrl}?` + searchParams, { method: "GET" }) ).json()) as IOfficeLocation; + // if(officeLocationData.result[0]!.adrGeoCodePostal.slice(0,2) !== "35") { + // return null; + // } + const role = await this.getRole(userData.typeLien.name); const userToAdd = { diff --git a/src/services/common/MailchimpService/MailchimpService.ts b/src/services/common/MailchimpService/MailchimpService.ts index a14b0b9b..a8e0de5f 100644 --- a/src/services/common/MailchimpService/MailchimpService.ts +++ b/src/services/common/MailchimpService/MailchimpService.ts @@ -1,9 +1,10 @@ import EmailRepository from "@Repositories/EmailRepository"; import BaseService from "@Services/BaseService"; -import { Emails } from "@prisma/client"; +import { Emails, PrismaClient } from "@prisma/client"; import { Service } from "typedi"; import MailchimpClient from "@mailchimp/mailchimp_transactional"; import { BackendVariables } from "@Common/config/variables/Variables"; +// import DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService"; @Service() export default class MailchimpService extends BaseService { @@ -113,4 +114,17 @@ export default class MailchimpService extends BaseService { }; }); } + + public async sendRecapEmails() { + const prisma = new PrismaClient(); + const usersToEmail = await prisma.$queryRaw + `SELECT DISTINCT c.email, c.civility, c.last_name + FROM Contacts c + JOIN Users u ON c.uid = u.contact_uid + JOIN office_folders of ON u.office_uid = of.office_uid + JOIN Documents d ON of.uid = d.folder_uid + WHERE d.document_status = 'DEPOSITED';` + + console.log(usersToEmail); + } }