-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add stage integartion to the connector
- Loading branch information
1 parent
f88d7e4
commit dcba333
Showing
7 changed files
with
193 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { IStageService } from '@crm/stage/types'; | ||
import { CrmObject } from '@crm/@lib/@types'; | ||
import { CloseStageOutput, commonStageCloseProperties } from './types'; | ||
import axios from 'axios'; | ||
import { PrismaService } from '@@core/prisma/prisma.service'; | ||
import { LoggerService } from '@@core/logger/logger.service'; | ||
import { ActionType, handleServiceError } from '@@core/utils/errors'; | ||
import { EncryptionService } from '@@core/encryption/encryption.service'; | ||
import { ApiResponse } from '@@core/utils/types'; | ||
import { ServiceRegistry } from '../registry.service'; | ||
|
||
@Injectable() | ||
export class CloseService implements IStageService { | ||
constructor( | ||
private prisma: PrismaService, | ||
private logger: LoggerService, | ||
private cryptoService: EncryptionService, | ||
private registry: ServiceRegistry, | ||
) { | ||
this.logger.setContext( | ||
CrmObject.stage.toUpperCase() + ':' + CloseService.name, | ||
); | ||
this.registry.registerService('close', this); | ||
} | ||
|
||
async syncStages( | ||
linkedUserId: string, | ||
deal_id: string, | ||
custom_properties?: string[], | ||
): Promise<ApiResponse<CloseStageOutput[]>> { | ||
try { | ||
const connection = await this.prisma.connections.findFirst({ | ||
where: { | ||
id_linked_user: linkedUserId, | ||
provider_slug: 'close', | ||
vertical: 'crm', | ||
}, | ||
}); | ||
|
||
const res = await this.prisma.crm_deals.findUnique({ | ||
where: { id_crm_deal: deal_id }, | ||
}); | ||
const baseURL = `${connection.account_url}/activity/status_change/opportunity/?opportunity_id=${res.remote_id}`; | ||
|
||
const resp = await axios.get(baseURL, { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${this.cryptoService.decrypt( | ||
connection.access_token, | ||
)}`, | ||
}, | ||
}); | ||
this.logger.log(`Synced close stages !`); | ||
return { | ||
data: resp?.data?.data, | ||
message: 'Close stages retrieved', | ||
statusCode: 200, | ||
}; | ||
} catch (error) { | ||
handleServiceError( | ||
error, | ||
this.logger, | ||
'Close', | ||
CrmObject.stage, | ||
ActionType.GET, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { CloseStageOutput, CloseStageInput } from './types'; | ||
import { | ||
UnifiedStageInput, | ||
UnifiedStageOutput, | ||
} from '@crm/stage/types/model.unified'; | ||
import { IStageMapper } from '@crm/stage/types'; | ||
|
||
export class CloseStageMapper implements IStageMapper { | ||
desunify( | ||
source: UnifiedStageInput, | ||
customFieldMappings?: { | ||
slug: string; | ||
remote_id: string; | ||
}[], | ||
): CloseStageInput { | ||
return {}; | ||
} | ||
|
||
unify( | ||
source: CloseStageOutput | CloseStageOutput[], | ||
customFieldMappings?: { | ||
slug: string; | ||
remote_id: string; | ||
}[], | ||
): UnifiedStageOutput | UnifiedStageOutput[] { | ||
if (!Array.isArray(source)) { | ||
return this.mapSingleStageToUnified(source, customFieldMappings); | ||
} | ||
// Handling array of CloseStageOutput | ||
return source.map((stage) => | ||
this.mapSingleStageToUnified(stage, customFieldMappings), | ||
); | ||
} | ||
|
||
private mapSingleStageToUnified( | ||
stage: CloseStageOutput, | ||
customFieldMappings?: { | ||
slug: string; | ||
remote_id: string; | ||
}[], | ||
): UnifiedStageOutput { | ||
const field_mappings: { [key: string]: any } = {}; | ||
if (customFieldMappings) { | ||
for (const mapping of customFieldMappings) { | ||
field_mappings[mapping.slug] = stage[mapping.remote_id]; | ||
} | ||
} | ||
return { | ||
remote_id: stage.id, | ||
stage_name: stage.new_status_label, | ||
field_mappings, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
export interface CloseStageInput { | ||
email?: string; | ||
firstname?: string; | ||
phone?: string; | ||
lastname?: string; | ||
city?: string; | ||
country?: string; | ||
zip?: string; | ||
state?: string; | ||
address?: string; | ||
mobilephone?: string; | ||
close_owner_id?: string; | ||
associatedcompanyid?: string; | ||
fax?: string; | ||
jobtitle?: string; | ||
[key: string]: any; | ||
} | ||
|
||
interface OpportunityStatusChange { | ||
organization_id: string; | ||
_type: string; | ||
contact_id: string | null; | ||
created_by: string; | ||
created_by_name: string; | ||
date_created: string; | ||
date_updated: string; | ||
lead_id: string; | ||
new_status_id: string; | ||
new_status_label: string; | ||
new_status_type: string; | ||
new_pipeline_id: string; | ||
old_status_id: string; | ||
old_status_label: string; | ||
old_status_type: string; | ||
old_pipeline_id: string; | ||
opportunity_date_won: string; | ||
opportunity_id: string; | ||
opportunity_value: number; | ||
opportunity_value_formatted: string | null; | ||
opportunity_value_currency: string; | ||
updated_by: string; | ||
updated_by_name: string; | ||
user_id: string; | ||
user_name: string; | ||
id: string; | ||
} | ||
|
||
export type CloseStageOutput = Partial<OpportunityStatusChange>; | ||
|
||
export const commonStageCloseProperties = { | ||
dealstage: '', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters