Skip to content

Commit

Permalink
🐛 Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
naelob committed Mar 25, 2024
1 parent 69950c9 commit ef6c8c6
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 59 deletions.
125 changes: 86 additions & 39 deletions packages/api/scripts/connectorUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ function updateFileContents(filePath, newContents) {
fs.writeFileSync(filePath, newContents);
}

function updateMappingsFile(mappingsFile, newServiceDirs, objectType) {
function updateMappingsFile(
mappingsFile,
newServiceDirs,
objectType,
vertical,
) {
let fileContent = fs.readFileSync(mappingsFile, 'utf8');

// Identify where the existing content before the first import starts, to preserve any comments or metadata at the start of the file
Expand All @@ -106,32 +111,39 @@ function updateMappingsFile(mappingsFile, newServiceDirs, objectType) {
let newImports = '';
let newInstances = '';
let newMappings = '';
newServiceDirs.forEach((newServiceName) => {
const serviceNameCapitalized =
newServiceName.charAt(0).toUpperCase() + newServiceName.slice(1);
const mapperClassName = `${serviceNameCapitalized}${objectType}Mapper`;
const mapperInstanceName = `${newServiceName.toLowerCase()}${objectType}Mapper`;

// Prepare the import statement and instance declaration
const importStatement = `import { ${mapperClassName} } from '../services/${newServiceName}/mappers';\n`;
const instanceDeclaration = `const ${mapperInstanceName} = new ${mapperClassName}();\n`;
const mappingEntry = ` ${newServiceName.toLowerCase()}: {\n unify: ${mapperInstanceName}.unify.bind(${mapperInstanceName}),\n desunify: ${mapperInstanceName}.desunify,\n },\n`;

// Check and append new import if it's not already present
if (!fileContent.includes(importStatement)) {
newImports += importStatement;
}

// Append instance declaration if not already present before the mapping object
if (!beforeMappingObject.includes(instanceDeclaration)) {
newInstances += instanceDeclaration;
}

// Prepare and append new mapping entry if not already present in the mapping object
if (!mappingObjectContent.includes(` ${newServiceName}: {`)) {
newMappings += mappingEntry;
}
});
newServiceDirs
.filter(
(newServiceName) =>
!(
vertical === 'ticketing' && newServiceName.toLowerCase() === 'zendesk'
),
)
.forEach((newServiceName) => {
const serviceNameCapitalized =
newServiceName.charAt(0).toUpperCase() + newServiceName.slice(1);
const mapperClassName = `${serviceNameCapitalized}${objectType}Mapper`;
const mapperInstanceName = `${newServiceName.toLowerCase()}${objectType}Mapper`;

// Prepare the import statement and instance declaration
const importStatement = `import { ${mapperClassName} } from '../services/${newServiceName}/mappers';\n`;
const instanceDeclaration = `const ${mapperInstanceName} = new ${mapperClassName}();\n`;
const mappingEntry = ` ${newServiceName.toLowerCase()}: {\n unify: ${mapperInstanceName}.unify.bind(${mapperInstanceName}),\n desunify: ${mapperInstanceName}.desunify,\n },\n`;

// Check and append new import if it's not already present
if (!fileContent.includes(importStatement)) {
newImports += importStatement;
}

// Append instance declaration if not already present before the mapping object
if (!beforeMappingObject.includes(instanceDeclaration)) {
newInstances += instanceDeclaration;
}

// Prepare and append new mapping entry if not already present in the mapping object
if (!mappingObjectContent.includes(` ${newServiceName}: {`)) {
newMappings += mappingEntry;
}
});

// Combine updates with the original sections of the file content
const updatedContentBeforeMapping =
Expand Down Expand Up @@ -208,6 +220,50 @@ function updateModuleFile(moduleFile, newServiceDirs) {
fs.writeFileSync(moduleFile, moduleFileContent);
}

function updateEnumFile(enumFilePath, newServiceDirs, vertical) {
let fileContent = fs.readFileSync(enumFilePath, 'utf8');
const base = vertical.substring(0, 1).toUpperCase() + vertical.substring(1);

// Define the enum name to be updated based on the vertical
let enumName = `${base}Providers`;
// Extract the enum content
const enumRegex = new RegExp(`export enum ${enumName} {([\\s\\S]*?)}\n`, 'm');
const match = fileContent.match(enumRegex);

if (match && match[1]) {
let enumEntries = match[1]
.trim()
.split(/\n/)
.map((e) => e.trim())
.filter((e) => e);
const existingEntries = enumEntries.map((entry) =>
entry.split('=')[0].trim(),
);

// Prepare new entries to be added
newServiceDirs.forEach((serviceName) => {
const enumEntryName = serviceName.toUpperCase(); // Assuming the enum entry name is the uppercase service name
if (!existingEntries.includes(enumEntryName)) {
// Format the new enum entry, assuming you want the name and value to be the same
enumEntries.push(`${enumEntryName} = '${serviceName}',`);
}
});

// Rebuild the enum content
const updatedEnumContent = `export enum ${enumName} {\n ${enumEntries.join(
'\n ',
)}\n}\n`;

// Replace the old enum content with the new one in the file content
fileContent = fileContent.replace(enumRegex, updatedEnumContent);

// Write the updated content back to the file
fs.writeFileSync(enumFilePath, fileContent);
//console.log(`Updated ${enumName} in ${enumFilePath}`);
} else {
console.error(`Could not find enum ${enumName} in file.`);
}
}
// Main script logic
function updateObjectTypes(baseDir, objectType, vertical) {
const __dirname = path.dirname(fileURLToPath(import.meta.url));
Expand All @@ -228,31 +284,22 @@ function updateObjectTypes(baseDir, objectType, vertical) {
providersFilePath,
`${vertical.toUpperCase()}_PROVIDERS`,
);
const currentEnum = extractArrayFromFile(enumFilePath, `ProviderVertical`);

// Compare the extracted arrays with the new service names
const newProviders = newServiceDirs.filter(
(service) => !currentProviders.includes(service),
);
const newEnum = newServiceDirs.filter(
(service) => !currentEnum.includes(service),
);

// Add any new services to the arrays
const updatedProviders = [...currentProviders, ...newProviders];
const updatedEnum = [...currentEnum, ...newEnum];

// Update the arrays in the files
updateArrayInFile(
providersFilePath,
`${vertical.toUpperCase()}_PROVIDERS`,
updatedProviders,
);
updateArrayInFile(
enumFilePath,
`${vertical.toUpperCase()}Providers`,
updatedEnum,
);

updateEnumFile(enumFilePath, newServiceDirs, vertical);
const moduleFile = path.join(
__dirname,
`../src/${vertical}/${objectType.toLowerCase()}/${objectType.toLowerCase()}.module.ts`,
Expand All @@ -267,7 +314,7 @@ function updateObjectTypes(baseDir, objectType, vertical) {
);

// Call updateMappingsFile to update the mappings file with new services
updateMappingsFile(mappingsFile, newServiceDirs, objectType);
updateMappingsFile(mappingsFile, newServiceDirs, objectType, vertical);

// Continue with the rest of the updateObjectTypes function...
const importStatements = generateImportStatements(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import {
ClickupTeamInput,
ClickupTeamOutput,
} from '@ticketing/team/services/clickup/types';

import {
FrontAccountInput,
FrontAccountOutput,
Expand Down Expand Up @@ -198,7 +193,6 @@ export type OriginalTagInput =
| JiraTagInput;
/* team */
export type OriginalTeamInput =
| ClickupTeamInput
| ZendeskTeamInput
| GithubTeamInput
| FrontTeamInput
Expand Down Expand Up @@ -268,7 +262,6 @@ export type OriginalTagOutput =

/* team */
export type OriginalTeamOutput =
| ClickupTeamOutput
| ZendeskTeamOutput
| GithubTeamOutput
| FrontTeamOutput
Expand Down
1 change: 0 additions & 1 deletion packages/api/src/ticketing/team/team.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import { GorgiasService } from './services/gorgias';
GithubService,
JiraService,
GorgiasService,
ClickupService,
],
exports: [SyncService],
})
Expand Down
12 changes: 1 addition & 11 deletions packages/api/src/ticketing/team/types/mappingsTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ClickupTeamMapper } from '../services/clickup/mappers';
import { JiraTeamMapper } from '../services/jira/mappers';
import { FrontTeamMapper } from '../services/front/mappers';
import { GithubTeamMapper } from '../services/github/mappers';
Expand All @@ -9,9 +8,8 @@ const zendeskTeamMapper = new ZendeskTeamMapper();
const frontTeamMapper = new FrontTeamMapper();
const githubTeamMapper = new GithubTeamMapper();
const gorgiasTeamMapper = new GorgiasTeamMapper();

const clickupTeamMapper = new ClickupTeamMapper();
const jiraTeamMapper = new JiraTeamMapper();

export const teamUnificationMapping = {
zendesk_tcg: {
unify: zendeskTeamMapper.unify.bind(zendeskTeamMapper),
Expand All @@ -29,16 +27,8 @@ export const teamUnificationMapping = {
unify: gorgiasTeamMapper.unify.bind(gorgiasTeamMapper),
desunify: gorgiasTeamMapper.desunify,
},
clickup: {
unify: clickupTeamMapper.unify.bind(clickupTeamMapper),
desunify: clickupTeamMapper.desunify,
},
jira: {
unify: jiraTeamMapper.unify.bind(jiraTeamMapper),
desunify: jiraTeamMapper.desunify,
},
zendesk: {
unify: zendeskTeamMapper.unify.bind(zendeskTeamMapper),
desunify: zendeskTeamMapper.desunify,
},
};
3 changes: 2 additions & 1 deletion packages/shared/src/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export const FILE_STORAGE_PROVIDERS = [''];

export function getProviderVertical(providerName: string): ProviderVertical {
if (CRM_PROVIDERS.includes(providerName)) {
return ProviderVertical.CRM; }
return ProviderVertical.CRM;
}
if (HRIS_PROVIDERS.includes(providerName)) {
return ProviderVertical.HRIS;
}
Expand Down

0 comments on commit ef6c8c6

Please sign in to comment.