Skip to content

Commit

Permalink
Merge pull request #467 from mit-27/fix-sync-feature
Browse files Browse the repository at this point in the history
Fix Sync Feature
  • Loading branch information
naelob authored Jun 4, 2024
2 parents 7b21d0b + 66aedfb commit 498efc4
Show file tree
Hide file tree
Showing 6 changed files with 2,663 additions and 2,530 deletions.
14 changes: 9 additions & 5 deletions packages/api/src/@core/connections/connections.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { ConnectorCategory } from '@panora/shared';
import { AccountingConnectionsService } from './accounting/services/accounting.connection.service';
import { MarketingAutomationConnectionsService } from './marketingautomation/services/marketingautomation.connection.service';
import { JwtAuthGuard } from '@@core/auth/guards/jwt-auth.guard';
import { CoreSyncService } from '@@core/sync/sync.service';

export type StateDataType = {
projectId: string;
Expand All @@ -34,6 +35,7 @@ export class ConnectionsController {
private readonly ticketingConnectionsService: TicketingConnectionsService,
private readonly accountingConnectionsService: AccountingConnectionsService,
private readonly marketingAutomationConnectionsService: MarketingAutomationConnectionsService,
private readonly coreSyncService: CoreSyncService,
private logger: LoggerService,
private prisma: PrismaService,
) {
Expand All @@ -49,7 +51,7 @@ export class ConnectionsController {
@ApiQuery({ name: 'location', required: true, type: String })
@ApiResponse({ status: 200 })
@Get('oauth/callback')
handleCallback(
async handleCallback(
@Res() res: Response,
@Query('state') state: string,
@Query('code') code: string,
Expand All @@ -71,7 +73,7 @@ export class ConnectionsController {
switch (vertical.toLowerCase()) {
case ConnectorCategory.Crm:
const zohoLocation_ = zohoLocation ? zohoLocation : '';
this.crmConnectionsService.handleCRMCallBack(
await this.crmConnectionsService.handleCRMCallBack(
projectId,
linkedUserId,
providerName,
Expand All @@ -82,7 +84,7 @@ export class ConnectionsController {
case ConnectorCategory.Ats:
break;
case ConnectorCategory.Accounting:
this.accountingConnectionsService.handleAccountingCallBack(
await this.accountingConnectionsService.handleAccountingCallBack(
projectId,
linkedUserId,
providerName,
Expand All @@ -94,22 +96,24 @@ export class ConnectionsController {
case ConnectorCategory.Hris:
break;
case ConnectorCategory.MarketingAutomation:
this.marketingAutomationConnectionsService.handleMarketingAutomationCallBack(
await this.marketingAutomationConnectionsService.handleMarketingAutomationCallBack(
projectId,
linkedUserId,
providerName,
code,
);
break;
case ConnectorCategory.Ticketing:
this.ticketingConnectionsService.handleTicketingCallBack(
await this.ticketingConnectionsService.handleTicketingCallBack(
projectId,
linkedUserId,
providerName,
code,
);
break;
}
// Performing Core Sync Service
this.coreSyncService.initialSync(vertical.toLowerCase(), providerName, linkedUserId, projectId);
res.redirect(returnUrl);
} catch (error) {
handleServiceError(error, this.logger);
Expand Down
37 changes: 35 additions & 2 deletions packages/api/src/@core/connections/connections.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ import { TicketingConnectionModule } from './ticketing/ticketing.connection.modu
import { AccountingConnectionModule } from './accounting/accounting.connection.module';
import { MarketingAutomationConnectionsModule } from './marketingautomation/marketingautomation.connection.module';
import { ValidateUserService } from '@@core/utils/services/validateUser.service';
import { CoreSyncService } from '@@core/sync/sync.service';
import { CompanyModule } from '@crm/company/company.module';
import { ContactModule } from '@crm/contact/contact.module';
import { DealModule } from '@crm/deal/deal.module';
import { EngagementModule } from '@crm/engagement/engagement.module';
import { NoteModule } from '@crm/note/note.module';
import { StageModule } from '@crm/stage/stage.module';
import { TaskModule } from '@crm/task/task.module';
import { UserModule } from '@crm/user/user.module';
import { AccountModule } from '@ticketing/account/account.module';
import { CollectionModule } from '@ticketing/collection/collection.module';
import { CommentModule } from '@ticketing/comment/comment.module';
import { ContactModule as TContactModule } from '@ticketing/contact/contact.module';
import { TagModule } from '@ticketing/tag/tag.module';
import { TeamModule } from '@ticketing/team/team.module';
import { TicketModule } from '@ticketing/ticket/ticket.module';
import { UserModule as TUserModule } from '@ticketing/user/user.module';

@Module({
controllers: [ConnectionsController],
Expand All @@ -15,13 +32,29 @@ import { ValidateUserService } from '@@core/utils/services/validateUser.service'
TicketingConnectionModule,
AccountingConnectionModule,
MarketingAutomationConnectionsModule,
CompanyModule,
ContactModule,
DealModule,
EngagementModule,
NoteModule,
StageModule,
TaskModule,
UserModule,
AccountModule,
CollectionModule,
CommentModule,
TContactModule,
TagModule,
TeamModule,
TicketModule,
TUserModule
],
providers: [LoggerService, PrismaService, ValidateUserService],
providers: [LoggerService, PrismaService, ValidateUserService, CoreSyncService],
exports: [
CrmConnectionModule,
TicketingConnectionModule,
AccountingConnectionModule,
MarketingAutomationConnectionsModule,
],
})
export class ConnectionsModule {}
export class ConnectionsModule { }
96 changes: 96 additions & 0 deletions packages/api/src/@core/sync/sync.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Injectable } from '@nestjs/common';
import { LoggerService } from '../logger/logger.service';
import { ConnectorCategory } from '@panora/shared';
import { CrmObject, ENGAGEMENTS_TYPE } from '@crm/@lib/@types';
import { PrismaService } from '@@core/prisma/prisma.service';
import { handleServiceError } from '@@core/utils/errors';
import { SyncService as CrmCompanySyncService } from '@crm/company/sync/sync.service';
import { SyncService as CrmContactSyncService } from '@crm/contact/sync/sync.service';
Expand All @@ -22,6 +25,7 @@ import { SyncService as TicketingUserSyncService } from '@ticketing/user/sync/sy
export class CoreSyncService {
constructor(
private logger: LoggerService,
private prisma: PrismaService,
private CrmCompanySyncService: CrmCompanySyncService,
private CrmContactSyncService: CrmContactSyncService,
private CrmDealSyncService: CrmDealSyncService,
Expand All @@ -42,9 +46,101 @@ export class CoreSyncService {
this.logger.setContext(CoreSyncService.name);
}

//Initial sync which will execute when connection is successfully established
async initialSync(
vertical: string,
provider: string,
linkedUserId: string,
id_project: string
) {
try {

const tasks = [];

switch (vertical) {
case ConnectorCategory.Crm:
// logic
tasks.push(() => this.CrmUserSyncService.syncUsersForLinkedUser(provider, linkedUserId, id_project));
tasks.push(() => this.CrmCompanySyncService.syncCompaniesForLinkedUser(provider, linkedUserId, id_project));
tasks.push(() => this.CrmContactSyncService.syncContactsForLinkedUser(provider, linkedUserId, id_project));
tasks.push(() => this.CrmDealSyncService.syncDealsForLinkedUser(provider, linkedUserId, id_project));

for (const type of ENGAGEMENTS_TYPE) {
tasks.push(() => this.CrmEngagementSyncService.syncEngagementsForLinkedUser(provider, linkedUserId, id_project, type));
}

tasks.push(() => this.CrmNoteSyncService.syncNotesForLinkedUser(provider, linkedUserId, id_project));
tasks.push(() => this.CrmTaskSyncService.syncTasksForLinkedUser(provider, linkedUserId, id_project));

for (const task of tasks) {
try {
await task();
} catch (error) {
handleServiceError(error, this.logger);
}
}

const deals = await this.prisma.crm_deals.findMany({
where: {
remote_platform: provider,
id_linked_user: linkedUserId,
},
});
for (const deal of deals) {
await this.CrmStageSyncService.syncStagesForLinkedUser(
provider,
linkedUserId,
id_project,
deal.id_crm_deal,
);
}
break;

case ConnectorCategory.Ticketing:
// logic
tasks.push(() => this.TicketingUserSyncService.syncUsersForLinkedUser(provider, linkedUserId, id_project));
tasks.push(() => this.TicketingAccountSyncService.syncAccountsForLinkedUser(provider, linkedUserId, id_project));
tasks.push(() => this.TicketingCollectionSyncService.syncCollectionsForLinkedUser(provider, linkedUserId, id_project));
tasks.push(() => this.TicketingTicketSyncService.syncTicketsForLinkedUser(provider, linkedUserId, id_project));
tasks.push(() => this.TicketingTeamSyncService.syncTeamsForLinkedUser(provider, linkedUserId, id_project));
tasks.push(() => this.TicketingContactSyncService.syncContactsForLinkedUser(provider, linkedUserId, id_project));

for (const task of tasks) {
try {
await task();
} catch (error) {
handleServiceError(error, this.logger);
}
}

const tickets = await this.prisma.tcg_tickets.findMany({
where: {
remote_platform: provider,
id_linked_user: linkedUserId,
},
});

for (const ticket of tickets) {
try {
await this.TicketingCommentSyncService.syncCommentsForLinkedUser(provider, linkedUserId, id_project, ticket.id_tcg_ticket);
await this.TicketingTagSyncService.syncTagsForLinkedUser(provider, linkedUserId, id_project, ticket.id_tcg_ticket);
} catch (error) {
handleServiceError(error, this.logger);
}
}

break;
}

} catch (error) {
handleServiceError(error, this.logger);
}
}

// we must have a sync_jobs table with 7 (verticals) rows, one of each is syncing details
async getSyncStatus(vertical: string) {
try {

} catch (error) {
handleServiceError(error, this.logger);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/ticketing/@lib/@utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class Utils {
id_tcg_collection: uuid,
},
});
if (!res) throw new Error(`tcg_contact not found for uuid ${uuid}`);
if (!res) return;
return res.remote_id;
} catch (error) {
throw new Error(error);
Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/ticketing/comment/services/gitlab/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class GitlabCommentMapper implements ICommentMapper {
'gitlab',
);
if (user_id) {
opts.user_id = user_id;
opts = { ...opts, user_id };
}
}

Expand All @@ -102,11 +102,11 @@ export class GitlabCommentMapper implements ICommentMapper {
// }
if (comment.noteable_id) {
const ticket_id = await this.utils.getTicketUuidFromRemoteId(
String(comment.noteable_iid),
String(comment.noteable_id),
'gitlab'
)
if (ticket_id) {
opts.ticket_id = ticket_id;
opts = { ...opts, ticket_id };

}
}
Expand Down
Loading

0 comments on commit 498efc4

Please sign in to comment.