Skip to content

Commit

Permalink
馃悰 Add gitlab/comment and fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mit-27 committed May 9, 2024
1 parent 6d88d1c commit d3a0199
Show file tree
Hide file tree
Showing 10 changed files with 438 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ import {
GitlabTicketInput,
GitlabTicketOutput,
} from '@ticketing/ticket/services/gitlab/types';
import {
GitlabCommentInput,
GitlabCommentOutput
} from '@ticketing/comment/services/gitlab/types'

/* INPUT */

Expand All @@ -140,7 +144,8 @@ export type OriginalCommentInput =
| ZendeskCommentInput
| FrontCommentInput
| GorgiasCommentInput
| JiraCommentInput;
| JiraCommentInput
| GitlabCommentInput;
//| JiraCommentServiceMgmtInput;
/* user */
export type OriginalUserInput =
Expand Down Expand Up @@ -203,7 +208,8 @@ export type OriginalCommentOutput =
| ZendeskCommentOutput
| FrontCommentOutput
| GorgiasCommentOutput
| JiraCommentOutput;
| JiraCommentOutput
| GitlabCommentOutput;
/* user */
export type OriginalUserOutput =
| ZendeskUserOutput
Expand Down
58 changes: 58 additions & 0 deletions packages/api/src/ticketing/@lib/@utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,62 @@ export class Utils {
throw new Error(error);
}
}

async getCollectionUuidFromRemoteId(remote_id: string, remote_platform: string) {
try {
const res = await this.prisma.tcg_collections.findFirst({
where: {
remote_id: remote_id,
remote_platform: remote_platform,
},
});
if (!res) return;
return res.id_tcg_collection;
} catch (error) {
throw new Error(error);
}
}

async getCollectionRemoteIdFromUuid(uuid: string) {
try {
const res = await this.prisma.tcg_collections.findFirst({
where: {
id_tcg_collection: uuid,
},
});
if (!res) throw new Error(`tcg_contact not found for uuid ${uuid}`);
return res.remote_id;
} catch (error) {
throw new Error(error);
}
}

async getTicketUuidFromRemoteId(remote_id: string, remote_platform: string) {
try {
const res = await this.prisma.tcg_tickets.findFirst({
where: {
remote_id: remote_id,
remote_platform: remote_platform,
},
});
if (!res) return;
return res.id_tcg_ticket;
} catch (error) {
throw new Error(error);
}
}

async getTicketRemoteIdFromUuid(uuid: string) {
try {
const res = await this.prisma.tcg_tickets.findFirst({
where: {
id_tcg_ticket: uuid,
},
});
if (!res) throw new Error(`tcg_contact not found for uuid ${uuid}`);
return res.remote_id;
} catch (error) {
throw new Error(error);
}
}
}
2 changes: 2 additions & 0 deletions packages/api/src/ticketing/comment/comment.module.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GitlabService } from './services/gitlab';
import { Module } from '@nestjs/common';
import { SyncService } from './sync/sync.service';
import { WebhookService } from '@@core/webhook/webhook.service';
Expand Down Expand Up @@ -38,6 +39,7 @@ import { GorgiasService } from './services/gorgias';
FrontService,
JiraService,
GorgiasService,
GitlabService,
],
exports: [
SyncService,
Expand Down
177 changes: 177 additions & 0 deletions packages/api/src/ticketing/comment/services/gitlab/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import { Injectable } from '@nestjs/common';
import { LoggerService } from '@@core/logger/logger.service';
import { PrismaService } from '@@core/prisma/prisma.service';
import { EncryptionService } from '@@core/encryption/encryption.service';
import { ApiResponse } from '@@core/utils/types';
import axios from 'axios';
import { ActionType, handleServiceError } from '@@core/utils/errors';
import { ICommentService } from '@ticketing/comment/types';
import { TicketingObject } from '@ticketing/@lib/@types';
import { GitlabCommentInput, GitlabCommentOutput } from './types';
import { ServiceRegistry } from '../registry.service';
import { Utils } from '@ticketing/@lib/@utils';;
import * as fs from 'fs';

@Injectable()
export class GitlabService implements ICommentService {
private readonly utils: Utils;

constructor(
private prisma: PrismaService,
private logger: LoggerService,
private cryptoService: EncryptionService,
private registry: ServiceRegistry,
) {
this.logger.setContext(
TicketingObject.comment.toUpperCase() + ':' + GitlabService.name,
);
this.registry.registerService('gitlab', this);
this.utils = new Utils();
}

async addComment(
commentData: GitlabCommentInput,
linkedUserId: string,
remoteIdTicket: string,
): Promise<ApiResponse<GitlabCommentOutput>> {
try {
const connection = await this.prisma.connections.findFirst({
where: {
id_linked_user: linkedUserId,
provider_slug: 'gitlab',
vertical: 'ticketing',
},
});

let uploads = [];
const uuids = commentData.attachment as any[];
if (uuids && uuids.length > 0) {
const attachmentPromises = uuids.map(async (uuid) => {
const res = await this.prisma.tcg_attachments.findUnique({
where: {
id_tcg_attachment: uuid.extra,
},
});
if (!res) {
throw new Error(`tcg_attachment not found for uuid ${uuid}`);
}
// Assuming you want to construct the right binary attachment here
// For now, we'll just return the URL
const stats = fs.statSync(res.file_url);
return {
url: res.file_url,
name: res.file_name,
size: stats.size,
content_type: 'application/pdf', //todo
};
});
uploads = await Promise.all(attachmentPromises);
}

// Assuming you want to modify the comment object here
// For now, we'll just add the uploads to the comment
const data = {
...commentData,
attachments: uploads,
};

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])






const resp = await axios.post(
`${connection.account_url}/projects/${remote_project_id}/issues/${remoteIdTicket}/notes`,
JSON.stringify(data),
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.cryptoService.decrypt(
connection.access_token,
)}`,
},
},
);

return {
data: resp.data,
message: 'Gitlab comment created',
statusCode: 201,
};
} catch (error) {
handleServiceError(
error,
this.logger,
'Gitlab',
TicketingObject.comment,
ActionType.POST,
);
}
}
async syncComments(
linkedUserId: string,
id_ticket: string,
): Promise<ApiResponse<GitlabCommentOutput[]>> {
try {
const connection = await this.prisma.connections.findFirst({
where: {
id_linked_user: linkedUserId,
provider_slug: 'gitlab',
vertical: 'ticketing',
},
});
//retrieve ticket remote id so we can retrieve the comments in the original software
const ticket = await this.prisma.tcg_tickets.findUnique({
where: {
id_tcg_ticket: id_ticket,
},
select: {
remote_id: true,
collections: true
},
});

// retrieve the remote_id of project from collections
const remote_project_id = await this.utils.getCollectionRemoteIdFromUuid(ticket.collections[0])


const resp = await axios.get(
`${connection.account_url}/projects/${remote_project_id}/issues/${ticket.remote_id}/notes`,
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.cryptoService.decrypt(
connection.access_token,
)}`,
},
},
);
this.logger.log(`Synced gitlab comments !`);

return {
data: resp.data._results,
message: 'Gitlab comments retrieved',
statusCode: 200,
};
} catch (error) {
handleServiceError(
error,
this.logger,
'Gitlab',
TicketingObject.comment,
ActionType.GET,
);
}
}
}
Loading

0 comments on commit d3a0199

Please sign in to comment.