Phone country fix (#9167)
This commit is contained in:
@ -5,42 +5,44 @@ interface CommandLoggerOptions {
|
|||||||
constructorName: string;
|
constructorName: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const isCommandLogger = (
|
||||||
|
logger: Logger | CommandLogger,
|
||||||
|
): logger is CommandLogger => {
|
||||||
|
return typeof logger['setVerbose'] === 'function';
|
||||||
|
};
|
||||||
|
|
||||||
export class CommandLogger {
|
export class CommandLogger {
|
||||||
private logger: Logger;
|
private logger: Logger;
|
||||||
private verbose: boolean;
|
private verboseFlag: boolean;
|
||||||
|
|
||||||
constructor(options: CommandLoggerOptions) {
|
constructor(options: CommandLoggerOptions) {
|
||||||
this.logger = new Logger(options.constructorName);
|
this.logger = new Logger(options.constructorName);
|
||||||
this.verbose = options.verbose ?? true;
|
this.verboseFlag = options.verbose ?? false;
|
||||||
}
|
}
|
||||||
|
|
||||||
log(message: string, context?: string) {
|
log(message: string, ...optionalParams: [...any, string?]) {
|
||||||
if (this.verbose) {
|
this.logger.log(message, ...optionalParams);
|
||||||
this.logger.log(message, context);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
error(message: string, stack?: string, context?: string) {
|
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) {
|
warn(message: string, context?: string) {
|
||||||
if (this.verbose) {
|
this.logger.warn(message, context);
|
||||||
this.logger.warn(message, context);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
debug(message: string, context?: string) {
|
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) {
|
setVerbose(flag: boolean) {
|
||||||
if (this.verbose) {
|
this.verboseFlag = flag;
|
||||||
this.logger.verbose(message, context);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import {
|
|||||||
ActiveWorkspacesCommandOptions,
|
ActiveWorkspacesCommandOptions,
|
||||||
ActiveWorkspacesCommandRunner,
|
ActiveWorkspacesCommandRunner,
|
||||||
} from 'src/database/commands/active-workspaces.command';
|
} 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 { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||||
import {
|
import {
|
||||||
FieldMetadataEntity,
|
FieldMetadataEntity,
|
||||||
@ -56,16 +57,19 @@ export class PhoneCallingCodeCreateColumnCommand extends ActiveWorkspacesCommand
|
|||||||
this.logger.log(
|
this.logger.log(
|
||||||
'Running command to add calling code and change country code with default one',
|
'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;
|
let workspaceIterator = 1;
|
||||||
|
|
||||||
for (const workspaceId of workspaceIds) {
|
for (const workspaceId of workspaceIds) {
|
||||||
this.logger.log(
|
this.logger.verbose(
|
||||||
`Running command for workspace ${workspaceId} ${workspaceIterator}/${workspaceIds.length}`,
|
`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`,
|
`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) {
|
for (const phoneFieldMetadata of phonesFieldMetadata) {
|
||||||
if (
|
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'}`,
|
`P1 Step 1 - Let's find the "nameSingular" of this objectMetadata: ${phoneFieldMetadata.object.nameSingular || 'not found'}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!phoneFieldMetadata.object?.nameSingular) continue;
|
if (!phoneFieldMetadata.object?.nameSingular) continue;
|
||||||
|
|
||||||
this.logger.log(
|
this.logger.verbose(
|
||||||
`P1 Step 1 - Create migration for field ${phoneFieldMetadata.name}`,
|
`P1 Step 1 - Create migration for field ${phoneFieldMetadata.name}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (options.dryRun === true) {
|
if (options.dryRun) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
await this.workspaceMigrationService.createCustomMigration(
|
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)}`,
|
`P1 Step 1 - RUN migration to create callingCodes for ${workspaceId.slice(0, 5)}`,
|
||||||
);
|
);
|
||||||
await this.workspaceMigrationRunnerService.executeMigrationFromPendingMigrations(
|
await this.workspaceMigrationRunnerService.executeMigrationFromPendingMigrations(
|
||||||
@ -134,7 +139,7 @@ export class PhoneCallingCodeCreateColumnCommand extends ActiveWorkspacesCommand
|
|||||||
workspaceId,
|
workspaceId,
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(`Error in workspace ${workspaceId} : ${error}`);
|
this.logger.log(`Error in workspace ${workspaceId} : ${error}`);
|
||||||
}
|
}
|
||||||
workspaceIterator++;
|
workspaceIterator++;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,6 +10,7 @@ import {
|
|||||||
ActiveWorkspacesCommandOptions,
|
ActiveWorkspacesCommandOptions,
|
||||||
ActiveWorkspacesCommandRunner,
|
ActiveWorkspacesCommandRunner,
|
||||||
} from 'src/database/commands/active-workspaces.command';
|
} 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 { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||||
import {
|
import {
|
||||||
FieldMetadataEntity,
|
FieldMetadataEntity,
|
||||||
@ -82,7 +83,10 @@ export class PhoneCallingCodeMigrateDataCommand extends ActiveWorkspacesCommandR
|
|||||||
'Running command to add calling code and change country code with default one',
|
'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;
|
let workspaceIterator = 1;
|
||||||
|
|
||||||
@ -91,7 +95,7 @@ export class PhoneCallingCodeMigrateDataCommand extends ActiveWorkspacesCommandR
|
|||||||
`Running command for workspace ${workspaceId} ${workspaceIterator}/${workspaceIds.length}`,
|
`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`,
|
`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?.name) &&
|
||||||
isDefined(phoneFieldMetadata.object)
|
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'}`,
|
`P1 Step 1 - Let's find the "nameSingular" of this objectMetadata: ${phoneFieldMetadata.object.nameSingular || 'not found'}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!phoneFieldMetadata.object?.nameSingular) continue;
|
if (!phoneFieldMetadata.object?.nameSingular) continue;
|
||||||
|
|
||||||
this.logger.log(
|
this.logger.verbose(
|
||||||
`P1 Step 1 - Create migration for field ${phoneFieldMetadata.name}`,
|
`P1 Step 1 - Create migration for field ${phoneFieldMetadata.name}`,
|
||||||
);
|
);
|
||||||
if (options.dryRun === false) {
|
if (!options.dryRun) {
|
||||||
await this.workspaceMigrationService.createCustomMigration(
|
await this.workspaceMigrationService.createCustomMigration(
|
||||||
generateMigrationName(
|
generateMigrationName(
|
||||||
`create-${phoneFieldMetadata.object.nameSingular}PrimaryPhoneCallingCode-for-field-${phoneFieldMetadata.name}`,
|
`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)}`,
|
`P1 Step 1 - RUN migration to create callingCodes for ${workspaceId.slice(0, 5)}`,
|
||||||
);
|
);
|
||||||
await this.workspaceMigrationRunnerService.executeMigrationFromPendingMigrations(
|
await this.workspaceMigrationRunnerService.executeMigrationFromPendingMigrations(
|
||||||
@ -159,20 +163,20 @@ export class PhoneCallingCodeMigrateDataCommand extends ActiveWorkspacesCommandR
|
|||||||
workspaceId,
|
workspaceId,
|
||||||
);
|
);
|
||||||
|
|
||||||
this.logger.log(
|
this.logger.verbose(
|
||||||
`P1 Step 2 - Migrations for callingCode must be first. Now can use twentyORMGlobalManager to update countryCode`,
|
`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)`,
|
`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`,
|
`P1 Step 4 (same time) - update all additioanl phones to add a country code following the same logic`,
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const phoneFieldMetadata of phonesFieldMetadata) {
|
for (const phoneFieldMetadata of phonesFieldMetadata) {
|
||||||
this.logger.log(`P1 Step 2 - for ${phoneFieldMetadata.name}`);
|
this.logger.verbose(`P1 Step 2 - for ${phoneFieldMetadata.name}`);
|
||||||
if (
|
if (
|
||||||
isDefined(phoneFieldMetadata) &&
|
isDefined(phoneFieldMetadata) &&
|
||||||
isDefined(phoneFieldMetadata.name)
|
isDefined(phoneFieldMetadata.name)
|
||||||
@ -209,7 +213,7 @@ export class PhoneCallingCodeMigrateDataCommand extends ActiveWorkspacesCommandR
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (options.dryRun === false) {
|
if (!options.dryRun) {
|
||||||
await repository.update(record.id, {
|
await repository.update(record.id, {
|
||||||
[`${phoneFieldMetadata.name}PrimaryPhoneCallingCode`]:
|
[`${phoneFieldMetadata.name}PrimaryPhoneCallingCode`]:
|
||||||
record[phoneFieldMetadata.name].primaryPhoneCountryCode,
|
record[phoneFieldMetadata.name].primaryPhoneCountryCode,
|
||||||
@ -226,12 +230,12 @@ export class PhoneCallingCodeMigrateDataCommand extends ActiveWorkspacesCommandR
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(`Error in workspace ${workspaceId} : ${error}`);
|
this.logger.log(`Error in workspace ${workspaceId} : ${error}`);
|
||||||
}
|
}
|
||||||
workspaceIterator++;
|
workspaceIterator++;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.log(`
|
this.logger.verbose(`
|
||||||
|
|
||||||
Part 2 - FieldMetadata`);
|
Part 2 - FieldMetadata`);
|
||||||
|
|
||||||
@ -241,7 +245,7 @@ export class PhoneCallingCodeMigrateDataCommand extends ActiveWorkspacesCommandR
|
|||||||
`Running command for workspace ${workspaceId} ${workspaceIterator}/${workspaceIds.length}`,
|
`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`,
|
`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, ''),
|
primaryPhoneCountryCode.replace(/["']/g, ''),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (options.dryRun === false) {
|
if (!options.dryRun) {
|
||||||
await this.fieldMetadataRepository.update(phoneFieldMetadata.id, {
|
if (!defaultValue.primaryPhoneCallingCode) {
|
||||||
defaultValue: {
|
await this.fieldMetadataRepository.update(phoneFieldMetadata.id, {
|
||||||
...defaultValue,
|
defaultValue: {
|
||||||
primaryPhoneCountryCode: countryCode
|
...defaultValue,
|
||||||
? `'${countryCode}'`
|
primaryPhoneCountryCode: countryCode
|
||||||
: "''",
|
? `'${countryCode}'`
|
||||||
primaryPhoneCallingCode: isCallingCode(
|
: "''",
|
||||||
primaryPhoneCountryCode.replace(/["']/g, ''),
|
primaryPhoneCallingCode: isCallingCode(
|
||||||
)
|
primaryPhoneCountryCode.replace(/["']/g, ''),
|
||||||
? primaryPhoneCountryCode
|
)
|
||||||
: "''",
|
? primaryPhoneCountryCode
|
||||||
},
|
: "''",
|
||||||
});
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(`Error in workspace ${workspaceId} : ${error}`);
|
this.logger.log(`Error in workspace ${workspaceId} : ${error}`);
|
||||||
}
|
}
|
||||||
workspaceIterator++;
|
workspaceIterator++;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user