Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix mappings backend #402

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions packages/api/src/crm/company/services/attio/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,12 @@ export class AttioCompanyMapper implements ICompanyMapper {
// Attio Company doest not have direct mapping of number of employees

if (customFieldMappings && source.field_mappings) {
for (const fieldMapping of source.field_mappings) {
for (const key in fieldMapping) {
const mapping = customFieldMappings.find(
(mapping) => mapping.slug === key,
);
if (mapping) {
result.values[mapping.remote_id] = fieldMapping[key];
}
for (const [k, v] of Object.entries(source.field_mappings)) {
const mapping = customFieldMappings.find(
(mapping) => mapping.slug === k,
);
if (mapping) {
result[mapping.remote_id] = v;
}
}
}
Expand Down Expand Up @@ -127,10 +125,12 @@ export class AttioCompanyMapper implements ICompanyMapper {
remote_id: string;
}[],
): Promise<UnifiedCompanyOutput> {
const field_mappings =
customFieldMappings?.map((mapping) => ({
[mapping.slug]: company.values[mapping.remote_id],
})) || [];
const field_mappings: { [key: string]: any } = {};
if (customFieldMappings) {
for (const mapping of customFieldMappings) {
field_mappings[mapping.slug] = company.values[mapping.remote_id];
}
}

let opts: any = {};

Expand Down
25 changes: 12 additions & 13 deletions packages/api/src/crm/company/services/hubspot/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ export class HubspotCompanyMapper implements ICompanyMapper {
}

if (customFieldMappings && source.field_mappings) {
for (const fieldMapping of source.field_mappings) {
for (const key in fieldMapping) {
const mapping = customFieldMappings.find(
(mapping) => mapping.slug === key,
);
if (mapping) {
result[mapping.remote_id] = fieldMapping[key];
}
for (const [k, v] of Object.entries(source.field_mappings)) {
const mapping = customFieldMappings.find(
(mapping) => mapping.slug === k,
);
if (mapping) {
result[mapping.remote_id] = v;
}
}
}
Expand Down Expand Up @@ -89,11 +87,12 @@ export class HubspotCompanyMapper implements ICompanyMapper {
remote_id: string;
}[],
): Promise<UnifiedCompanyOutput> {
const field_mappings =
customFieldMappings?.map((mapping) => ({
[mapping.slug]: company.properties[mapping.remote_id],
})) || [];

const field_mappings: { [key: string]: any } = {};
if (customFieldMappings) {
for (const mapping of customFieldMappings) {
field_mappings[mapping.slug] = company.properties[mapping.remote_id];
}
}
let opts: any = {};
if (company.properties.hubspot_owner_id) {
const owner_id = await this.utils.getUserUuidFromRemoteId(
Expand Down
23 changes: 13 additions & 10 deletions packages/api/src/crm/company/services/pipedrive/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ export class PipedriveCompanyMapper implements ICompanyMapper {
};
}
}

if (customFieldMappings && source.field_mappings) {
customFieldMappings.forEach((mapping) => {
const customValue = source.field_mappings.find((f) => f[mapping.slug]);
if (customValue) {
result[mapping.remote_id] = customValue[mapping.slug];
for (const [k, v] of Object.entries(source.field_mappings)) {
const mapping = customFieldMappings.find(
(mapping) => mapping.slug === k,
);
if (mapping) {
result[mapping.remote_id] = v;
}
});
}
}

return result;
Expand Down Expand Up @@ -83,10 +84,12 @@ export class PipedriveCompanyMapper implements ICompanyMapper {
remote_id: string;
}[],
): Promise<UnifiedCompanyOutput> {
const field_mappings =
customFieldMappings?.map((mapping) => ({
[mapping.slug]: company[mapping.remote_id],
})) || [];
const field_mappings: { [key: string]: any } = {};
if (customFieldMappings) {
for (const mapping of customFieldMappings) {
field_mappings[mapping.slug] = company[mapping.remote_id];
}
}

let res = {
name: company.name,
Expand Down
24 changes: 13 additions & 11 deletions packages/api/src/crm/company/services/zendesk/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,12 @@ export class ZendeskCompanyMapper implements ICompanyMapper {
}
}
if (customFieldMappings && source.field_mappings) {
for (const fieldMapping of source.field_mappings) {
for (const key in fieldMapping) {
const mapping = customFieldMappings.find(
(mapping) => mapping.slug === key,
);
if (mapping) {
result.custom_fields[mapping.remote_id] = fieldMapping[key];
}
for (const [k, v] of Object.entries(source.field_mappings)) {
const mapping = customFieldMappings.find(
(mapping) => mapping.slug === k,
);
if (mapping) {
result[mapping.remote_id] = v;
}
}
}
Expand Down Expand Up @@ -92,9 +90,13 @@ export class ZendeskCompanyMapper implements ICompanyMapper {
remote_id: string;
}[],
): Promise<UnifiedCompanyOutput> {
const field_mappings = customFieldMappings.map((mapping) => ({
[mapping.slug]: company.custom_fields[mapping.remote_id],
}));
const field_mappings: { [key: string]: any } = {};
if (customFieldMappings) {
for (const mapping of customFieldMappings) {
field_mappings[mapping.slug] = company.custom_fields[mapping.remote_id];
}
}

// Constructing the email and phone details
const email_addresses = company.email
? [{ email_address: company.email, email_address_type: 'primary' }]
Expand Down
23 changes: 13 additions & 10 deletions packages/api/src/crm/company/services/zoho/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ export class ZohoCompanyMapper implements ICompanyMapper {
)?.email_address;
}

// Custom field mappings
if (customFieldMappings && source.field_mappings) {
customFieldMappings.forEach((mapping) => {
const customValue = source.field_mappings.find((f) => f[mapping.slug]);
if (customValue) {
result[mapping.remote_id] = customValue[mapping.slug];
for (const [k, v] of Object.entries(source.field_mappings)) {
const mapping = customFieldMappings.find(
(mapping) => mapping.slug === k,
);
if (mapping) {
result[mapping.remote_id] = v;
}
});
}
}

return result;
Expand Down Expand Up @@ -71,10 +72,12 @@ export class ZohoCompanyMapper implements ICompanyMapper {
remote_id: string;
}[],
): UnifiedCompanyOutput {
const field_mappings =
customFieldMappings?.map((mapping) => ({
[mapping.slug]: company[mapping.remote_id],
})) || [];
const field_mappings: { [key: string]: any } = {};
if (customFieldMappings) {
for (const mapping of customFieldMappings) {
field_mappings[mapping.slug] = company[mapping.remote_id];
}
}

return {
name: company.Account_Name,
Expand Down
8 changes: 3 additions & 5 deletions packages/api/src/crm/company/sync/sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,10 @@ export class SyncService implements OnModuleInit {
},
});

for (const mapping of company.field_mappings) {
for (const [slug, value] of Object.entries(company.field_mappings)) {
const attribute = await this.prisma.attribute.findFirst({
where: {
slug: Object.keys(mapping)[0],
slug: slug,
source: originSource,
id_consumer: linkedUserId,
},
Expand All @@ -418,9 +418,7 @@ export class SyncService implements OnModuleInit {
await this.prisma.value.create({
data: {
id_value: uuidv4(),
data: Object.values(mapping)[0]
? Object.values(mapping)[0]
: 'null',
data: value || 'null',
attribute: {
connect: {
id_attribute: attribute.id_attribute,
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/crm/company/types/model.unified.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ export class UnifiedCompanyInput {
phone_numbers?: Phone[];

@ApiPropertyOptional({
type: [{}],
type: {},
description:
'The custom field mappings of the company between the remote 3rd party & Panora',
})
field_mappings?: Record<string, any>[];
field_mappings?: Record<string, any>;
}

export class UnifiedCompanyOutput extends UnifiedCompanyInput {
Expand Down
23 changes: 12 additions & 11 deletions packages/api/src/crm/contact/services/attio/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,12 @@ export class AttioContactMapper implements IContactMapper {
// }

if (customFieldMappings && source.field_mappings) {
for (const fieldMapping of source.field_mappings) {
for (const key in fieldMapping) {
const mapping = customFieldMappings.find(
(mapping) => mapping.slug === key,
);
if (mapping) {
result[mapping.remote_id] = fieldMapping[key];
}
for (const [k, v] of Object.entries(source.field_mappings)) {
const mapping = customFieldMappings.find(
(mapping) => mapping.slug === k,
);
if (mapping) {
result[mapping.remote_id] = v;
}
}
}
Expand Down Expand Up @@ -101,9 +99,12 @@ export class AttioContactMapper implements IContactMapper {
remote_id: string;
}[],
): Promise<UnifiedContactOutput> {
const field_mappings = customFieldMappings.map((mapping) => ({
[mapping.slug]: contact.values[mapping.remote_id],
}));
const field_mappings: { [key: string]: any } = {};
if (customFieldMappings) {
for (const mapping of customFieldMappings) {
field_mappings[mapping.slug] = contact.values[mapping.remote_id];
}
}
const address: Address = {
street_1: '',
city: '',
Expand Down
8 changes: 5 additions & 3 deletions packages/api/src/crm/contact/services/contact.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,12 @@ export class ContactService {
},
});

for (const mapping of target_contact.field_mappings) {
for (const [slug, value] of Object.entries(
target_contact.field_mappings,
)) {
const attribute = await this.prisma.attribute.findFirst({
where: {
slug: Object.keys(mapping)[0],
slug: slug,
source: integrationId,
id_consumer: linkedUserId,
},
Expand All @@ -340,7 +342,7 @@ export class ContactService {
await this.prisma.value.create({
data: {
id_value: uuidv4(),
data: Object.values(mapping)[0] || 'null',
data: value || 'null',
attribute: {
connect: {
id_attribute: attribute.id_attribute,
Expand Down
25 changes: 14 additions & 11 deletions packages/api/src/crm/contact/services/hubspot/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,16 @@ export class HubspotContactMapper implements IContactMapper {
}

if (customFieldMappings && source.field_mappings) {
for (const fieldMapping of source.field_mappings) {
for (const key in fieldMapping) {
const mapping = customFieldMappings.find(
(mapping) => mapping.slug === key,
);
if (mapping) {
result[mapping.remote_id] = fieldMapping[key];
}
for (const [k, v] of Object.entries(source.field_mappings)) {
const mapping = customFieldMappings.find(
(mapping) => mapping.slug === k,
);
if (mapping) {
result[mapping.remote_id] = v;
}
}
}

return result;
}

Expand All @@ -81,9 +80,13 @@ export class HubspotContactMapper implements IContactMapper {
remote_id: string;
}[],
): UnifiedContactOutput {
const field_mappings = customFieldMappings.map((mapping) => ({
[mapping.slug]: contact.properties[mapping.remote_id],
}));
const field_mappings: { [key: string]: any } = {};
if (customFieldMappings) {
for (const mapping of customFieldMappings) {
field_mappings[mapping.slug] = contact.properties[mapping.remote_id];
}
}

/*todo: const address: Address = {
street_1: '',
city: '',
Expand Down
23 changes: 12 additions & 11 deletions packages/api/src/crm/contact/services/pipedrive/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ export class PipedriveContactMapper implements IContactMapper {
}

if (customFieldMappings && source.field_mappings) {
for (const fieldMapping of source.field_mappings) {
for (const key in fieldMapping) {
const mapping = customFieldMappings.find(
(mapping) => mapping.slug === key,
);
if (mapping) {
result[mapping.remote_id] = fieldMapping[key];
}
for (const [k, v] of Object.entries(source.field_mappings)) {
const mapping = customFieldMappings.find(
(mapping) => mapping.slug === k,
);
if (mapping) {
result[mapping.remote_id] = v;
}
}
}
Expand Down Expand Up @@ -94,9 +92,12 @@ export class PipedriveContactMapper implements IContactMapper {
remote_id: string;
}[],
): Promise<UnifiedContactOutput> {
const field_mappings = customFieldMappings.map((mapping) => ({
[mapping.slug]: contact[mapping.remote_id],
}));
const field_mappings: { [key: string]: any } = {};
if (customFieldMappings) {
for (const mapping of customFieldMappings) {
field_mappings[mapping.slug] = contact[mapping.remote_id];
}
}
const address: Address = {
street_1: '',
city: '',
Expand Down
Loading
Loading