Phone country fix (#9167)

This commit is contained in:
Guillim
2024-12-20 15:28:17 +01:00
committed by GitHub
parent d08075f610
commit 928c99a725
3 changed files with 69 additions and 56 deletions

View File

@ -5,42 +5,44 @@ interface CommandLoggerOptions {
constructorName: string;
}
export const isCommandLogger = (
logger: Logger | CommandLogger,
): logger is CommandLogger => {
return typeof logger['setVerbose'] === 'function';
};
export class CommandLogger {
private logger: Logger;
private verbose: boolean;
private verboseFlag: boolean;
constructor(options: CommandLoggerOptions) {
this.logger = new Logger(options.constructorName);
this.verbose = options.verbose ?? true;
this.verboseFlag = options.verbose ?? false;
}
log(message: string, context?: string) {
if (this.verbose) {
this.logger.log(message, context);
}
log(message: string, ...optionalParams: [...any, string?]) {
this.logger.log(message, ...optionalParams);
}
error(message: string, stack?: string, context?: string) {
if (this.verbose) {
this.logger.error(message, stack, context);
}
this.logger.error(message, stack, context);
}
warn(message: string, context?: string) {
if (this.verbose) {
this.logger.warn(message, context);
}
this.logger.warn(message, context);
}
debug(message: string, context?: string) {
if (this.verbose) {
this.logger.debug(message, context);
this.logger.debug(message, context);
}
verbose(message: string, ...optionalParams: [...any, string?]) {
if (this.verboseFlag) {
this.logger.log(message, ...optionalParams);
}
}
verboseLog(message: string, context?: string) {
if (this.verbose) {
this.logger.verbose(message, context);
}
setVerbose(flag: boolean) {
this.verboseFlag = flag;
}
}

View File

@ -9,6 +9,7 @@ import {
ActiveWorkspacesCommandOptions,
ActiveWorkspacesCommandRunner,
} from 'src/database/commands/active-workspaces.command';
import { isCommandLogger } from 'src/database/commands/logger';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import {
FieldMetadataEntity,
@ -56,16 +57,19 @@ export class PhoneCallingCodeCreateColumnCommand extends ActiveWorkspacesCommand
this.logger.log(
'Running command to add calling code and change country code with default one',
);
if (isCommandLogger(this.logger)) {
this.logger.setVerbose(options.verbose ?? false);
}
this.logger.log(`Part 1 - Workspace`);
this.logger.verbose(`Part 1 - Workspace`);
let workspaceIterator = 1;
for (const workspaceId of workspaceIds) {
this.logger.log(
this.logger.verbose(
`Running command for workspace ${workspaceId} ${workspaceIterator}/${workspaceIds.length}`,
);
this.logger.log(
this.logger.verbose(
`P1 Step 1 - let's find all the fieldsMetadata that have the PHONES type, and extract the objectMetadataId`,
);
@ -80,19 +84,20 @@ export class PhoneCallingCodeCreateColumnCommand extends ActiveWorkspacesCommand
for (const phoneFieldMetadata of phonesFieldMetadata) {
if (
isDefined(phoneFieldMetadata?.name && phoneFieldMetadata.object)
isDefined(phoneFieldMetadata?.name) &&
isDefined(phoneFieldMetadata.object)
) {
this.logger.log(
this.logger.verbose(
`P1 Step 1 - Let's find the "nameSingular" of this objectMetadata: ${phoneFieldMetadata.object.nameSingular || 'not found'}`,
);
if (!phoneFieldMetadata.object?.nameSingular) continue;
this.logger.log(
this.logger.verbose(
`P1 Step 1 - Create migration for field ${phoneFieldMetadata.name}`,
);
if (options.dryRun === true) {
if (options.dryRun) {
continue;
}
await this.workspaceMigrationService.createCustomMigration(
@ -123,7 +128,7 @@ export class PhoneCallingCodeCreateColumnCommand extends ActiveWorkspacesCommand
}
}
this.logger.log(
this.logger.verbose(
`P1 Step 1 - RUN migration to create callingCodes for ${workspaceId.slice(0, 5)}`,
);
await this.workspaceMigrationRunnerService.executeMigrationFromPendingMigrations(
@ -134,7 +139,7 @@ export class PhoneCallingCodeCreateColumnCommand extends ActiveWorkspacesCommand
workspaceId,
);
} catch (error) {
console.log(`Error in workspace ${workspaceId} : ${error}`);
this.logger.log(`Error in workspace ${workspaceId} : ${error}`);
}
workspaceIterator++;
}

View File

@ -10,6 +10,7 @@ import {
ActiveWorkspacesCommandOptions,
ActiveWorkspacesCommandRunner,
} from 'src/database/commands/active-workspaces.command';
import { isCommandLogger } from 'src/database/commands/logger';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import {
FieldMetadataEntity,
@ -82,7 +83,10 @@ export class PhoneCallingCodeMigrateDataCommand extends ActiveWorkspacesCommandR
'Running command to add calling code and change country code with default one',
);
this.logger.log(`Part 1 - Workspace`);
if (isCommandLogger(this.logger)) {
this.logger.setVerbose(options.verbose ?? false);
}
this.logger.verbose(`Part 1 - Workspace`);
let workspaceIterator = 1;
@ -91,7 +95,7 @@ export class PhoneCallingCodeMigrateDataCommand extends ActiveWorkspacesCommandR
`Running command for workspace ${workspaceId} ${workspaceIterator}/${workspaceIds.length}`,
);
this.logger.log(
this.logger.verbose(
`P1 Step 1 - let's find all the fieldsMetadata that have the PHONES type, and extract the objectMetadataId`,
);
@ -109,16 +113,16 @@ export class PhoneCallingCodeMigrateDataCommand extends ActiveWorkspacesCommandR
isDefined(phoneFieldMetadata?.name) &&
isDefined(phoneFieldMetadata.object)
) {
this.logger.log(
this.logger.verbose(
`P1 Step 1 - Let's find the "nameSingular" of this objectMetadata: ${phoneFieldMetadata.object.nameSingular || 'not found'}`,
);
if (!phoneFieldMetadata.object?.nameSingular) continue;
this.logger.log(
this.logger.verbose(
`P1 Step 1 - Create migration for field ${phoneFieldMetadata.name}`,
);
if (options.dryRun === false) {
if (!options.dryRun) {
await this.workspaceMigrationService.createCustomMigration(
generateMigrationName(
`create-${phoneFieldMetadata.object.nameSingular}PrimaryPhoneCallingCode-for-field-${phoneFieldMetadata.name}`,
@ -148,7 +152,7 @@ export class PhoneCallingCodeMigrateDataCommand extends ActiveWorkspacesCommandR
}
}
this.logger.log(
this.logger.verbose(
`P1 Step 1 - RUN migration to create callingCodes for ${workspaceId.slice(0, 5)}`,
);
await this.workspaceMigrationRunnerService.executeMigrationFromPendingMigrations(
@ -159,20 +163,20 @@ export class PhoneCallingCodeMigrateDataCommand extends ActiveWorkspacesCommandR
workspaceId,
);
this.logger.log(
this.logger.verbose(
`P1 Step 2 - Migrations for callingCode must be first. Now can use twentyORMGlobalManager to update countryCode`,
);
this.logger.log(
this.logger.verbose(
`P1 Step 3 (same time) - update CountryCode to letters: +33 => FR || +1 => US (if mulitple, first one)`,
);
this.logger.log(
this.logger.verbose(
`P1 Step 4 (same time) - update all additioanl phones to add a country code following the same logic`,
);
for (const phoneFieldMetadata of phonesFieldMetadata) {
this.logger.log(`P1 Step 2 - for ${phoneFieldMetadata.name}`);
this.logger.verbose(`P1 Step 2 - for ${phoneFieldMetadata.name}`);
if (
isDefined(phoneFieldMetadata) &&
isDefined(phoneFieldMetadata.name)
@ -209,7 +213,7 @@ export class PhoneCallingCodeMigrateDataCommand extends ActiveWorkspacesCommandR
};
});
}
if (options.dryRun === false) {
if (!options.dryRun) {
await repository.update(record.id, {
[`${phoneFieldMetadata.name}PrimaryPhoneCallingCode`]:
record[phoneFieldMetadata.name].primaryPhoneCountryCode,
@ -226,12 +230,12 @@ export class PhoneCallingCodeMigrateDataCommand extends ActiveWorkspacesCommandR
}
}
} catch (error) {
console.log(`Error in workspace ${workspaceId} : ${error}`);
this.logger.log(`Error in workspace ${workspaceId} : ${error}`);
}
workspaceIterator++;
}
this.logger.log(`
this.logger.verbose(`
Part 2 - FieldMetadata`);
@ -241,7 +245,7 @@ export class PhoneCallingCodeMigrateDataCommand extends ActiveWorkspacesCommandR
`Running command for workspace ${workspaceId} ${workspaceIterator}/${workspaceIds.length}`,
);
this.logger.log(
this.logger.verbose(
`P2 Step 1 - let's find all the fieldsMetadata that have the PHONES type, and extract the objectMetadataId`,
);
@ -276,24 +280,26 @@ export class PhoneCallingCodeMigrateDataCommand extends ActiveWorkspacesCommandR
primaryPhoneCountryCode.replace(/["']/g, ''),
);
if (options.dryRun === false) {
await this.fieldMetadataRepository.update(phoneFieldMetadata.id, {
defaultValue: {
...defaultValue,
primaryPhoneCountryCode: countryCode
? `'${countryCode}'`
: "''",
primaryPhoneCallingCode: isCallingCode(
primaryPhoneCountryCode.replace(/["']/g, ''),
)
? primaryPhoneCountryCode
: "''",
},
});
if (!options.dryRun) {
if (!defaultValue.primaryPhoneCallingCode) {
await this.fieldMetadataRepository.update(phoneFieldMetadata.id, {
defaultValue: {
...defaultValue,
primaryPhoneCountryCode: countryCode
? `'${countryCode}'`
: "''",
primaryPhoneCallingCode: isCallingCode(
primaryPhoneCountryCode.replace(/["']/g, ''),
)
? primaryPhoneCountryCode
: "''",
},
});
}
}
}
} catch (error) {
console.log(`Error in workspace ${workspaceId} : ${error}`);
this.logger.log(`Error in workspace ${workspaceId} : ${error}`);
}
workspaceIterator++;
}