diff --git a/packages/api/prisma/schema.prisma b/packages/api/prisma/schema.prisma index e74db881c..bb781d7d6 100644 --- a/packages/api/prisma/schema.prisma +++ b/packages/api/prisma/schema.prisma @@ -389,3 +389,12 @@ model events { @@index([id_linked_user], map: "fk_linkeduserid_projectid") } + +/// This model or at least one of its fields has comments in the database, and requires an additional setup for migrations: Read more: https://pris.ly/d/database-comments +model remote_data { + id_remote_data String @id(map: "pk_remote_data") @db.Uuid + ressource_owner_id String? @unique(map: "force_unique_ressourceownerid") @db.Uuid + format String? + data String? + created_at DateTime? @db.Timestamp(6) +} diff --git a/packages/api/src/crm/contact/services/contact.service.ts b/packages/api/src/crm/contact/services/contact.service.ts index abca1e8ba..37868908f 100644 --- a/packages/api/src/crm/contact/services/contact.service.ts +++ b/packages/api/src/crm/contact/services/contact.service.ts @@ -1,4 +1,4 @@ -import { HttpStatus, Injectable } from '@nestjs/common'; +import { Injectable } from '@nestjs/common'; import { PrismaService } from '@@core/prisma/prisma.service'; import { FreshSalesService } from './freshsales'; import { HubspotService } from './hubspot'; @@ -198,7 +198,6 @@ export class ContactService { remote_data?: boolean, ): Promise> { try { - // handle case for remote data too //TODO: handle case where data is not there (not synced) or old synced const job_resp_create = await this.prisma.events.create({ data: { @@ -270,19 +269,28 @@ export class ContactService { }), ); - const res: ContactResponse = { + let res: ContactResponse = { contacts: unifiedContacts, }; - //TODO - /*if (remote_data) { - const resp = await this.prisma.remote_data - + if (remote_data) { + const remote_array_data: Record[] = await Promise.all( + contacts.map(async (contact) => { + const resp = await this.prisma.remote_data.findFirst({ + where: { + ressource_owner_id: contact.id_crm_contact, + }, + }); + const remote_data = JSON.parse(resp.data); + return remote_data; + }), + ); + res = { ...res, - remote_data: [resp.data], + remote_data: remote_array_data, }; - }*/ + } await this.prisma.events.update({ where: { id_event: job_id, diff --git a/packages/api/src/crm/contact/sync/sync.service.ts b/packages/api/src/crm/contact/sync/sync.service.ts index b37b85cea..6fe87e00e 100644 --- a/packages/api/src/crm/contact/sync/sync.service.ts +++ b/packages/api/src/crm/contact/sync/sync.service.ts @@ -180,20 +180,23 @@ export class SyncContactsService implements OnModuleInit { } } - //TODO: insert remote_data in db - /*await this.prisma.remote_data.upsert({ + //insert remote_data in db + await this.prisma.remote_data.upsert({ where: { - id_crm_contact: unique_crm_contact_id, + ressource_owner_id: unique_crm_contact_id, }, create: { id_remote_data: uuidv4(), - id_crm_contact: unique_crm_contact_id, - data: remote_data, + ressource_owner_id: unique_crm_contact_id, + format: 'json', + data: JSON.stringify(remote_data[i]), + created_at: new Date(), }, update: { - data: remote_data, + data: JSON.stringify(remote_data[i]), + created_at: new Date(), }, - });*/ + }); } } catch (error) { handleServiceError(error, this.logger);