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

Phone country fix #9167

Merged
merged 35 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
ac4f119
backend primaryPhoneCallingCode
guillim Dec 11, 2024
d5fa458
creating the migration step 1
guillim Dec 11, 2024
01f773c
new plan
guillim Dec 12, 2024
bbc6023
migration step 2
guillim Dec 13, 2024
2edb073
P1 - step 1, 2 and 3 done
guillim Dec 16, 2024
13f68ee
addiitonal phones done
guillim Dec 16, 2024
1abcae8
part 2 - fieldmetadata
guillim Dec 16, 2024
c5ccf54
bug fix clearing cache
guillim Dec 16, 2024
b060833
seeds
guillim Dec 16, 2024
ba096f7
adding try catch
guillim Dec 17, 2024
ceea9ca
seeder correction
guillim Dec 17, 2024
f0945a5
fix seeder
guillim Dec 17, 2024
1c5daf8
Merge branch 'main' of https://github.com/twentyhq/twenty into phone-…
guillim Dec 17, 2024
f74717a
greptile fix
guillim Dec 17, 2024
7487a3d
comment the primaryPhoneCallingCode to enable retrocompatibility
guillim Dec 17, 2024
754921f
split command to create calling code column
guillim Dec 18, 2024
848a587
putting back the calling code in the backend phone type definition
guillim Dec 18, 2024
9f39d1f
phone-country-front-work
guillim Dec 18, 2024
93b7cbf
test updade
guillim Dec 18, 2024
359f2d1
Merge branch 'main' of https://github.com/twentyhq/twenty into phone-…
guillim Dec 18, 2024
d1c6c16
typing issues
guillim Dec 18, 2024
9c0a5b8
Weiko's review suggestions
guillim Dec 19, 2024
5215738
dryRun
guillim Dec 19, 2024
b5401c5
CommandLogger
guillim Dec 19, 2024
9be49c8
Merge branch 'main' of https://github.com/twentyhq/twenty into phone-…
guillim Dec 19, 2024
5529a24
dryrun fix
guillim Dec 19, 2024
e5f6159
verbose and custom logger
guillim Dec 20, 2024
13c1864
logger fix twice print
guillim Dec 20, 2024
48d692f
Merge branch 'main' of https://github.com/twentyhq/twenty into phone-…
guillim Dec 20, 2024
e0ab767
reorder
guillim Dec 20, 2024
077eafd
removing tsdoc
guillim Dec 20, 2024
6ef6b8b
verbose better handling for child commands
guillim Dec 20, 2024
cfcfb62
fix idemPotent
guillim Dec 20, 2024
7f982ac
greptile
guillim Dec 20, 2024
93221e1
back in the same order
guillim Dec 20, 2024
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
38 changes: 20 additions & 18 deletions packages/twenty-server/src/database/commands/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

verboseLog(message: string, context?: string) {
if (this.verbose) {
this.logger.verbose(message, context);
verbose(message: string, ...optionalParams: [...any, string?]) {
if (this.verboseFlag) {
this.logger.log(message, ...optionalParams);
}
guillim marked this conversation as resolved.
Show resolved Hide resolved
}

setVerbose(flag: boolean) {
this.verboseFlag = flag;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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`,
);

Expand All @@ -82,17 +86,17 @@ export class PhoneCallingCodeCreateColumnCommand extends ActiveWorkspacesCommand
if (
isDefined(phoneFieldMetadata?.name && phoneFieldMetadata.object)
guillim marked this conversation as resolved.
Show resolved Hide resolved
) {
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(
Expand Down Expand Up @@ -123,7 +127,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(
Expand All @@ -134,7 +138,7 @@ export class PhoneCallingCodeCreateColumnCommand extends ActiveWorkspacesCommand
workspaceId,
);
} catch (error) {
console.log(`Error in workspace ${workspaceId} : ${error}`);
this.logger.log(`Error in workspace ${workspaceId} : ${error}`);
guillim marked this conversation as resolved.
Show resolved Hide resolved
}
workspaceIterator++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;

Expand All @@ -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`,
);

Expand All @@ -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}`,
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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`);

Expand All @@ -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`,
);

Expand Down Expand Up @@ -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}`);
guillim marked this conversation as resolved.
Show resolved Hide resolved
}
workspaceIterator++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ export class UpgradeTo0_40Command extends ActiveWorkspacesCommandRunner {
'Running command to upgrade to 0.40: must start with phone calling code otherwise SyncMetadata will fail',
);

await this.recordPositionBackfillCommand.executeActiveWorkspacesCommand(
await this.phoneCallingCodeCreateColumnCommand.executeActiveWorkspacesCommand(
guillim marked this conversation as resolved.
Show resolved Hide resolved
passedParam,
options,
workspaceIds,
);

await this.phoneCallingCodeCreateColumnCommand.executeActiveWorkspacesCommand(
await this.phoneCallingCodeMigrateDataCommand.executeActiveWorkspacesCommand(
passedParam,
options,
workspaceIds,
);

await this.phoneCallingCodeMigrateDataCommand.executeActiveWorkspacesCommand(
await this.recordPositionBackfillCommand.executeActiveWorkspacesCommand(
passedParam,
options,
workspaceIds,
Expand Down
Loading