From 7f12892303c59a088c290b91aa6c75ba18c19bcf Mon Sep 17 00:00:00 2001 From: mit-27 Date: Fri, 10 May 2024 12:04:45 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20bugs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/services/gitlab/index.ts | 47 ++++++++++++++----- .../comment/services/gitlab/mappers.ts | 11 +++-- .../ticketing/comment/sync/sync.service.ts | 17 +++---- .../ticketing/ticket/services/gitlab/index.ts | 4 +- .../ticket/services/gitlab/mappers.ts | 6 +-- .../ticket/services/ticket.service.ts | 1 + .../src/ticketing/ticket/sync/sync.service.ts | 10 ++-- 7 files changed, 64 insertions(+), 32 deletions(-) diff --git a/packages/api/src/ticketing/comment/services/gitlab/index.ts b/packages/api/src/ticketing/comment/services/gitlab/index.ts index 80bfa23c9..e045cfb27 100644 --- a/packages/api/src/ticketing/comment/services/gitlab/index.ts +++ b/packages/api/src/ticketing/comment/services/gitlab/index.ts @@ -75,24 +75,38 @@ export class GitlabService implements ICommentService { attachments: uploads, }; - const ticket = await this.prisma.tcg_tickets.findUnique({ + const ticket = await this.prisma.tcg_tickets.findFirst({ where: { - id_tcg_ticket: remoteIdTicket, + remote_id: remoteIdTicket, + remote_platform: 'gitlab', }, select: { - collections: true - }, + collections: true, + id_tcg_ticket: true + } }); - const remote_project_id = await this.utils.getCollectionRemoteIdFromUuid(ticket.collections[0]) - - - + // const ticket = await this.prisma.tcg_tickets.findUnique({ + // where: { + // id_tcg_ticket: remoteIdTicket, + // }, + // select: { + // collections: true + // }, + // }); + const remote_project_id = await this.utils.getCollectionRemoteIdFromUuid(ticket.collections[0]); + // Retrieve the uuid of issue from remote_data + const remote_data = await this.prisma.remote_data.findFirst({ + where: { + ressource_owner_id: ticket.id_tcg_ticket, + }, + }); + const { iid } = JSON.parse(remote_data.data); const resp = await axios.post( - `${connection.account_url}/projects/${remote_project_id}/issues/${remoteIdTicket}/notes`, + `${connection.account_url}/projects/${remote_project_id}/issues/${iid}/notes`, JSON.stringify(data), { headers: { @@ -145,9 +159,19 @@ export class GitlabService implements ICommentService { // retrieve the remote_id of project from collections const remote_project_id = await this.utils.getCollectionRemoteIdFromUuid(ticket.collections[0]) + // Retrieve the uuid of issue from remote_data + const remote_data = await this.prisma.remote_data.findFirst({ + where: { + ressource_owner_id: id_ticket, + }, + }); + const { iid } = JSON.parse(remote_data.data); + + console.log("Requested URL : ", `${connection.account_url}/projects/${remote_project_id}/issues/${iid}/notes`) + const resp = await axios.get( - `${connection.account_url}/projects/${remote_project_id}/issues/${ticket.remote_id}/notes`, + `${connection.account_url}/projects/${remote_project_id}/issues/${iid}/notes`, { headers: { 'Content-Type': 'application/json', @@ -158,9 +182,10 @@ export class GitlabService implements ICommentService { }, ); this.logger.log(`Synced gitlab comments !`); + console.log(resp.data) return { - data: resp.data._results, + data: resp.data, message: 'Gitlab comments retrieved', statusCode: 200, }; diff --git a/packages/api/src/ticketing/comment/services/gitlab/mappers.ts b/packages/api/src/ticketing/comment/services/gitlab/mappers.ts index 72ee3df10..086bdec7c 100644 --- a/packages/api/src/ticketing/comment/services/gitlab/mappers.ts +++ b/packages/api/src/ticketing/comment/services/gitlab/mappers.ts @@ -83,7 +83,9 @@ export class GitlabCommentMapper implements ICommentMapper { String(comment.author.id), 'gitlab', ); - opts.user_id = user_id; + if (user_id) { + opts.user_id = user_id; + } } // if (user_id) { @@ -100,10 +102,13 @@ export class GitlabCommentMapper implements ICommentMapper { // } if (comment.noteable_id) { const ticket_id = await this.utils.getTicketUuidFromRemoteId( - String(comment.noteable_id), + String(comment.noteable_iid), 'gitlab' ) - opts.ticket_id = ticket_id; + if (ticket_id) { + opts.ticket_id = ticket_id; + + } } const res: UnifiedCommentOutput = { diff --git a/packages/api/src/ticketing/comment/sync/sync.service.ts b/packages/api/src/ticketing/comment/sync/sync.service.ts index d180fb8df..98f13c20a 100644 --- a/packages/api/src/ticketing/comment/sync/sync.service.ts +++ b/packages/api/src/ticketing/comment/sync/sync.service.ts @@ -60,7 +60,7 @@ export class SyncService implements OnModuleInit { } //function used by sync worker which populate our tcg_comments table //its role is to fetch all comments from providers 3rd parties and save the info inside our db - //@Cron('*/2 * * * *') // every 2 minutes (for testing) + // @Cron('*/2 * * * *') // every 2 minutes (for testing) @Cron('0 */8 * * *') // every 8 hours async syncComments(user_id?: string) { try { @@ -189,9 +189,9 @@ export class SyncService implements OnModuleInit { data: { id_event: uuidv4(), status: 'success', - type: 'ticketing.comment.synced', - method: 'SYNC', - url: '/sync', + type: 'ticketing.comment.pulled', + method: 'PULL', + url: '/pull', provider: integrationId, direction: '0', timestamp: new Date(), @@ -200,7 +200,7 @@ export class SyncService implements OnModuleInit { }); await this.webhook.handleWebhook( comments_data, - 'ticketing.comment.synced', + 'ticketing.comment.pulled', id_project, event.id_event, ); @@ -236,15 +236,16 @@ export class SyncService implements OnModuleInit { let unique_ticketing_comment_id: string; const opts = - comment.creator_type === 'contact' + (comment.creator_type === 'CONTACT' && comment.contact_id) ? { id_tcg_contact: comment.contact_id, } - : comment.creator_type === 'user' + : (comment.creator_type === 'USER' && comment.user_id) ? { id_tcg_user: comment.user_id, } - : {}; //case where nothing is passed for creator or a not authorized value; + : {}; + //case where nothing is passed for creator or a not authorized value; if (existingComment) { // Update the existing comment diff --git a/packages/api/src/ticketing/ticket/services/gitlab/index.ts b/packages/api/src/ticketing/ticket/services/gitlab/index.ts index 36d2b8341..b3552c9df 100644 --- a/packages/api/src/ticketing/ticket/services/gitlab/index.ts +++ b/packages/api/src/ticketing/ticket/services/gitlab/index.ts @@ -32,7 +32,7 @@ export class GitlabService implements ITicketService { const connection = await this.prisma.connections.findFirst({ where: { id_linked_user: linkedUserId, - provider_slug: 'github', + provider_slug: 'gitlab', vertical: 'ticketing', }, }); @@ -73,7 +73,7 @@ export class GitlabService implements ITicketService { const connection = await this.prisma.connections.findFirst({ where: { id_linked_user: linkedUserId, - provider_slug: 'github', + provider_slug: 'gitlab', vertical: 'ticketing', }, }); diff --git a/packages/api/src/ticketing/ticket/services/gitlab/mappers.ts b/packages/api/src/ticketing/ticket/services/gitlab/mappers.ts index 8008809ed..31adc9029 100644 --- a/packages/api/src/ticketing/ticket/services/gitlab/mappers.ts +++ b/packages/api/src/ticketing/ticket/services/gitlab/mappers.ts @@ -98,7 +98,7 @@ export class GitlabTicketMapper implements ITicketMapper { let opts: any; if (ticket.type) { - opts.type = ticket.type === "opened" ? "OPEN" : "CLOSED" + opts = { ...opts, type: ticket.type === "opened" ? "OPEN" : "CLOSED" } } if (ticket.assignee) { @@ -108,7 +108,7 @@ export class GitlabTicketMapper implements ITicketMapper { 'gitlab', ); if (user_id) { - opts = { assigned_to: [user_id] }; + opts = { ...opts, assigned_to: [user_id] }; } } @@ -118,7 +118,7 @@ export class GitlabTicketMapper implements ITicketMapper { 'gitlab' ); if (tcg_collection_id) { - opts = { project_id: tcg_collection_id } + opts = { ...opts, project_id: tcg_collection_id } } } diff --git a/packages/api/src/ticketing/ticket/services/ticket.service.ts b/packages/api/src/ticketing/ticket/services/ticket.service.ts index e1f819edf..ceeb62afc 100644 --- a/packages/api/src/ticketing/ticket/services/ticket.service.ts +++ b/packages/api/src/ticketing/ticket/services/ticket.service.ts @@ -468,6 +468,7 @@ export class TicketService { completed_at: ticket.completed_at || null, priority: ticket.priority || '', assigned_to: ticket.assigned_to || [], + collections: ticket.collections || [], field_mappings: field_mappings, }; }), diff --git a/packages/api/src/ticketing/ticket/sync/sync.service.ts b/packages/api/src/ticketing/ticket/sync/sync.service.ts index 9b7aeaf33..6cac7cf2a 100644 --- a/packages/api/src/ticketing/ticket/sync/sync.service.ts +++ b/packages/api/src/ticketing/ticket/sync/sync.service.ts @@ -60,7 +60,7 @@ export class SyncService implements OnModuleInit { } //function used by sync worker which populate our tcg_tickets table //its role is to fetch all contacts from providers 3rd parties and save the info inside our db - //@Cron('*/2 * * * *') // every 2 minutes (for testing) + // @Cron('*/2 * * * *') // every 2 minutes (for testing) @Cron('0 */8 * * *') // every 8 hours async syncTickets(user_id?: string) { try { @@ -178,9 +178,9 @@ export class SyncService implements OnModuleInit { data: { id_event: uuidv4(), status: 'success', - type: 'ticketing.ticket.synced', - method: 'SYNC', - url: '/sync', + type: 'ticketing.ticket.pulled', + method: 'PULL', + url: '/pull', provider: integrationId, direction: '0', timestamp: new Date(), @@ -190,7 +190,7 @@ export class SyncService implements OnModuleInit { await this.webhook.handleWebhook( tickets_data, - 'ticketing.ticket.synced', + 'ticketing.ticket.pulled', id_project, event.id_event, );