From c0cb5a87e7af42bc4369ceff027532f0296dba9f Mon Sep 17 00:00:00 2001 From: Sergei Ogorelkov Date: Tue, 30 May 2023 10:28:36 +0400 Subject: [PATCH] [UBER-196] Fix duplicated project ids when creating a project (#3247) Signed-off-by: Sergei Ogorelkov --- models/core/src/tx.ts | 2 + packages/core/src/operations.ts | 8 +- packages/core/src/tx.ts | 5 + .../components/projects/ChangeIdentity.svelte | 24 ++-- .../components/projects/CreateProject.svelte | 103 +++++++++++++++--- server/core/src/storage.ts | 9 ++ 6 files changed, 118 insertions(+), 33 deletions(-) diff --git a/models/core/src/tx.ts b/models/core/src/tx.ts index 37333bb2a0..51d0f794b9 100644 --- a/models/core/src/tx.ts +++ b/models/core/src/tx.ts @@ -96,5 +96,7 @@ export class TTxApplyIf extends TTx implements TxApplyIf { // All matches should be true with at least one document. match!: DocumentClassQuery[] + // All matches should be false for all documents. + notMatch!: DocumentClassQuery[] txes!: TxCUD[] } diff --git a/packages/core/src/operations.ts b/packages/core/src/operations.ts index 44228ca7eb..0296a7e07c 100644 --- a/packages/core/src/operations.ts +++ b/packages/core/src/operations.ts @@ -281,6 +281,7 @@ export class TxOperations implements Omit { export class ApplyOperations extends TxOperations { txes: TxCUD[] = [] matches: DocumentClassQuery[] = [] + notMatches: DocumentClassQuery[] = [] constructor (readonly ops: TxOperations, readonly scope: string) { const txClient: Client = { getHierarchy: () => ops.client.getHierarchy(), @@ -303,10 +304,15 @@ export class ApplyOperations extends TxOperations { return this } + notMatch(_class: Ref>, query: DocumentQuery): ApplyOperations { + this.notMatches.push({ _class, query }) + return this + } + async commit (): Promise { if (this.txes.length > 0) { return await ((await this.ops.tx( - this.ops.txFactory.createTxApplyIf(core.space.Tx, this.scope, this.matches, this.txes) + this.ops.txFactory.createTxApplyIf(core.space.Tx, this.scope, this.matches, this.notMatches, this.txes) )) as Promise) } return true diff --git a/packages/core/src/tx.ts b/packages/core/src/tx.ts index d351e853d2..540bcfb00f 100644 --- a/packages/core/src/tx.ts +++ b/packages/core/src/tx.ts @@ -120,6 +120,9 @@ export interface TxApplyIf extends Tx { // All matches should be true with at least one document. match: DocumentClassQuery[] + // All matches should be false for all documents. + notMatch: DocumentClassQuery[] + // If all matched execute following transactions. txes: TxCUD[] } @@ -586,6 +589,7 @@ export class TxFactory { space: Ref, scope: string, match: DocumentClassQuery[], + notMatch: DocumentClassQuery[], txes: TxCUD[], modifiedOn?: Timestamp, modifiedBy?: Ref @@ -599,6 +603,7 @@ export class TxFactory { objectSpace: space, scope, match, + notMatch, txes } } diff --git a/plugins/tracker-resources/src/components/projects/ChangeIdentity.svelte b/plugins/tracker-resources/src/components/projects/ChangeIdentity.svelte index 795a9475f0..551c527020 100644 --- a/plugins/tracker-resources/src/components/projects/ChangeIdentity.svelte +++ b/plugins/tracker-resources/src/components/projects/ChangeIdentity.svelte @@ -1,34 +1,26 @@ { dispatch('close') }} @@ -36,7 +28,7 @@ >
- +
diff --git a/plugins/tracker-resources/src/components/projects/CreateProject.svelte b/plugins/tracker-resources/src/components/projects/CreateProject.svelte index d9c29f0e31..b192e95558 100644 --- a/plugins/tracker-resources/src/components/projects/CreateProject.svelte +++ b/plugins/tracker-resources/src/components/projects/CreateProject.svelte @@ -15,9 +15,17 @@ 0 && !(members.length === 0 && isPrivate)} + canSave={name.length > 0 && + identifier.length > 0 && + !projectsIdentifiers.has(identifier) && + !(members.length === 0 && isPrivate)} accentHeader width={'medium'} gap={'gapV-6'} - on:close={() => { - dispatch('close') - }} + onCancel={close} on:changeContent >
@@ -217,7 +274,7 @@
-
+
{#if !isNew} -
+ {:else if !isSaving && projectsIdentifiers.has(identifier)} +
+
{/if}
@@ -305,3 +368,11 @@
+ + diff --git a/server/core/src/storage.ts b/server/core/src/storage.ts index 9a795d4a6e..1352e040b1 100644 --- a/server/core/src/storage.ts +++ b/server/core/src/storage.ts @@ -594,6 +594,15 @@ class TServerStorage implements ServerStorage { break } } + if (passed) { + for (const { _class, query } of applyIf.notMatch) { + const res = await findAll(ctx, _class, query, { limit: 1 }) + if (res.length > 0) { + passed = false + break + } + } + } return { passed, onEnd } }