Skip to content

Commit

Permalink
✨ Zendesk ticketing working
Browse files Browse the repository at this point in the history
  • Loading branch information
naelob committed Jan 8, 2024
1 parent cde2962 commit 8a18f09
Show file tree
Hide file tree
Showing 48 changed files with 801 additions and 601 deletions.
Binary file added apps/webapp/public/providers/crm/zendesk_tcg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 16 additions & 33 deletions packages/api/src/crm/contact/services/contact.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class ContactService {
integrationId: string,
linkedUserId: string,
remote_data?: boolean,
): Promise<ContactResponse> {
): Promise<UnifiedContactOutput[]> {
try {
const responses = await Promise.all(
unifiedContactData.map((unifiedData) =>
Expand All @@ -48,15 +48,7 @@ export class ContactService {
),
);

const allContacts = responses.flatMap((response) => response.contacts);
const allRemoteData = responses.flatMap(
(response) => response.remote_data || [],
);

return {
contacts: allContacts,
remote_data: allRemoteData,
};
return responses;
} catch (error) {
handleServiceError(error, this.logger);
}
Expand All @@ -67,7 +59,7 @@ export class ContactService {
integrationId: string,
linkedUserId: string,
remote_data?: boolean,
): Promise<ContactResponse> {
): Promise<UnifiedContactOutput> {
try {
const linkedUser = await this.prisma.linked_users.findUnique({
where: {
Expand Down Expand Up @@ -258,7 +250,6 @@ export class ContactService {
});
}

/////
const result_contact = await this.getContact(
unique_crm_contact_id,
remote_data,
Expand All @@ -279,7 +270,7 @@ export class ContactService {
},
});
await this.webhook.handleWebhook(
result_contact.contacts,
result_contact,
'crm.contact.created',
linkedUser.id_project,
event.id_event,
Expand All @@ -293,7 +284,7 @@ export class ContactService {
async getContact(
id_crm_contact: string,
remote_data?: boolean,
): Promise<ContactResponse> {
): Promise<UnifiedContactOutput> {
try {
const contact = await this.prisma.crm_contacts.findUnique({
where: {
Expand Down Expand Up @@ -345,10 +336,7 @@ export class ContactService {
field_mappings: field_mappings,
};

let res: ContactResponse = {
contacts: [unifiedContact],
};

let res: UnifiedContactOutput = unifiedContact;
if (remote_data) {
const resp = await this.prisma.remote_data.findFirst({
where: {
Expand All @@ -359,7 +347,7 @@ export class ContactService {

res = {
...res,
remote_data: [remote_data],
remote_data: remote_data,
};
}

Expand All @@ -373,13 +361,13 @@ export class ContactService {
integrationId: string,
linkedUserId: string,
remote_data?: boolean,
): Promise<ContactResponse> {
): Promise<UnifiedContactOutput[]> {
try {
//TODO: handle case where data is not there (not synced) or old synced

const contacts = await this.prisma.crm_contacts.findMany({
where: {
remote_id: integrationId.toLowerCase(),
remote_platform: integrationId.toLowerCase(),
id_linked_user: linkedUserId,
},
include: {
Expand Down Expand Up @@ -432,27 +420,22 @@ export class ContactService {
}),
);

let res: ContactResponse = {
contacts: unifiedContacts,
};
let res: UnifiedContactOutput[] = unifiedContacts;

if (remote_data) {
const remote_array_data: Record<string, any>[] = await Promise.all(
contacts.map(async (contact) => {
const remote_array_data: UnifiedContactOutput[] = await Promise.all(
res.map(async (contact) => {
const resp = await this.prisma.remote_data.findFirst({
where: {
ressource_owner_id: contact.id_crm_contact,
ressource_owner_id: contact.id,
},
});
const remote_data = JSON.parse(resp.data);
return remote_data;
return { ...contact, remote_data };
}),
);

res = {
...res,
remote_data: remote_array_data,
};
res = remote_array_data;
}
const event = await this.prisma.events.create({
data: {
Expand All @@ -476,7 +459,7 @@ export class ContactService {
async updateContact(
id: string,
updateContactData: Partial<UnifiedContactInput>,
): Promise<ContactResponse> {
): Promise<UnifiedContactOutput> {
try {
} catch (error) {
handleServiceError(error, this.logger);
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/crm/contact/types/model.unified.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export class UnifiedContactOutput extends UnifiedContactInput {
description: 'The id of the contact in the context of the Crm software',
})
remote_id?: string;
remote_data?: Record<string, any>;
}
2 changes: 1 addition & 1 deletion packages/api/src/ticketing/@utils/@types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export enum TicketingObject {
ticket = 'ticket',
comment = 'comment',
user = 'user',
attachment = 'attachement',
attachment = 'attachment',
contact = 'contact',
account = 'account',
tag = 'tag',
Expand Down
29 changes: 11 additions & 18 deletions packages/api/src/ticketing/account/services/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class AccountService {
async getAccount(
id_ticketing_account: string,
remote_data?: boolean,
): Promise<AccountResponse> {
): Promise<UnifiedAccountOutput> {
try {
const account = await this.prisma.tcg_accounts.findUnique({
where: {
Expand Down Expand Up @@ -55,9 +55,7 @@ export class AccountService {
field_mappings: field_mappings,
};

let res: AccountResponse = {
accounts: [unifiedAccount],
};
let res: UnifiedAccountOutput = unifiedAccount;

if (remote_data) {
const resp = await this.prisma.remote_data.findFirst({
Expand All @@ -69,7 +67,7 @@ export class AccountService {

res = {
...res,
remote_data: [remote_data],
remote_data: remote_data,
};
}

Expand All @@ -83,13 +81,13 @@ export class AccountService {
integrationId: string,
linkedUserId: string,
remote_data?: boolean,
): Promise<AccountResponse> {
): Promise<UnifiedAccountOutput[]> {
try {
//TODO: handle case where data is not there (not synced) or old synced

const accounts = await this.prisma.tcg_accounts.findMany({
where: {
remote_id: integrationId.toLowerCase(),
remote_platform: integrationId.toLowerCase(),
id_linked_user: linkedUserId,
},
});
Expand Down Expand Up @@ -130,27 +128,22 @@ export class AccountService {
}),
);

let res: AccountResponse = {
accounts: unifiedAccounts,
};
let res: UnifiedAccountOutput[] = unifiedAccounts;

if (remote_data) {
const remote_array_data: Record<string, any>[] = await Promise.all(
accounts.map(async (account) => {
const remote_array_data: UnifiedAccountOutput[] = await Promise.all(
res.map(async (account) => {
const resp = await this.prisma.remote_data.findFirst({
where: {
ressource_owner_id: account.id_tcg_account,
ressource_owner_id: account.id,
},
});
const remote_data = JSON.parse(resp.data);
return remote_data;
return { ...account, remote_data };
}),
);

res = {
...res,
remote_data: remote_array_data,
};
res = remote_array_data;
}
const event = await this.prisma.events.create({
data: {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/ticketing/account/sync/sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class SyncService implements OnModuleInit {
}
}

@Cron('*/20 * * * *')
//@Cron('*/20 * * * *')
//function used by sync worker which populate our tcg_accounts table
//its role is to fetch all accounts from providers 3rd parties and save the info inside our db
async syncAccounts() {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/ticketing/account/types/mappingsTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const frontAccountMapper = new FrontAccountMapper();
const githubAccountMapper = new GithubAccountMapper();

export const accountUnificationMapping = {
zendesk: {
zendesk_tcg: {
unify: zendeskAccountMapper.unify,
desunify: zendeskAccountMapper.desunify,
},
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/ticketing/account/types/model.unified.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export class UnifiedAccountInput {
export class UnifiedAccountOutput extends UnifiedAccountInput {
id?: string;
remote_id?: string;
remote_data?: Record<string, any>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class AttachmentService {
integrationId: string,
linkedUserId: string,
remote_data?: boolean,
): Promise<AttachmentResponse> {
): Promise<UnifiedAttachmentOutput[]> {
try {
const responses = await Promise.all(
unifiedAttachmentData.map((unifiedData) =>
Expand All @@ -39,17 +39,7 @@ export class AttachmentService {
),
);

const allAttachments = responses.flatMap(
(response) => response.attachments,
);
const allRemoteData = responses.flatMap(
(response) => response.remote_data || [],
);

return {
attachments: allAttachments,
remote_data: allRemoteData,
};
return responses;
} catch (error) {
handleServiceError(error, this.logger);
}
Expand All @@ -60,7 +50,7 @@ export class AttachmentService {
integrationId: string,
linkedUserId: string,
remote_data?: boolean,
): Promise<AttachmentResponse> {
): Promise<UnifiedAttachmentOutput> {
try {
const linkedUser = await this.prisma.linked_users.findUnique({
where: {
Expand Down Expand Up @@ -136,7 +126,7 @@ export class AttachmentService {
});

await this.webhook.handleWebhook(
result_attachment.attachments,
result_attachment,
'ticketing.attachment.created',
linkedUser.id_project,
event.id_event,
Expand All @@ -150,7 +140,7 @@ export class AttachmentService {
async getAttachment(
id_ticketing_attachment: string,
remote_data?: boolean,
): Promise<AttachmentResponse> {
): Promise<UnifiedAttachmentOutput> {
try {
const attachment = await this.prisma.tcg_attachments.findUnique({
where: {
Expand Down Expand Up @@ -191,9 +181,7 @@ export class AttachmentService {
field_mappings: field_mappings,
};

let res: AttachmentResponse = {
attachments: [unifiedAttachment],
};
let res: UnifiedAttachmentOutput = unifiedAttachment;

if (remote_data) {
const resp = await this.prisma.remote_data.findFirst({
Expand All @@ -205,7 +193,7 @@ export class AttachmentService {

res = {
...res,
remote_data: [remote_data],
remote_data: remote_data,
};
}

Expand All @@ -219,12 +207,12 @@ export class AttachmentService {
integrationId: string,
linkedUserId: string,
remote_data?: boolean,
): Promise<AttachmentResponse> {
): Promise<UnifiedAttachmentOutput[]> {
try {
//TODO: handle case where data is not there (not synced) or old synced
const attachments = await this.prisma.tcg_attachments.findMany({
where: {
remote_id: integrationId.toLowerCase(),
remote_platform: integrationId.toLowerCase(),
id_linked_user: linkedUserId,
},
});
Expand Down Expand Up @@ -266,27 +254,22 @@ export class AttachmentService {
}),
);

let res: AttachmentResponse = {
attachments: unifiedAttachments,
};
let res: UnifiedAttachmentOutput[] = unifiedAttachments;

if (remote_data) {
const remote_array_data: Record<string, any>[] = await Promise.all(
attachments.map(async (attachment) => {
const remote_array_data: UnifiedAttachmentOutput[] = await Promise.all(
res.map(async (attachment) => {
const resp = await this.prisma.remote_data.findFirst({
where: {
ressource_owner_id: attachment.id_tcg_attachment,
ressource_owner_id: attachment.id,
},
});
const remote_data = JSON.parse(resp.data);
return remote_data;
return { ...attachment, remote_data };
}),
);

res = {
...res,
remote_data: remote_array_data,
};
res = remote_array_data;
}

const event = await this.prisma.events.create({
Expand All @@ -313,7 +296,7 @@ export class AttachmentService {
async downloadAttachment(
id_ticketing_attachment: string,
remote_data?: boolean,
): Promise<AttachmentResponse> {
): Promise<UnifiedAttachmentOutput> {
return;
}
}
Loading

0 comments on commit 8a18f09

Please sign in to comment.