Skip to content

Commit f87b0e2

Browse files
authored
ezqms-1193: fix issues with drafting a controlled doc version from effective (hcengineering#6535)
Signed-off-by: Alexey Zinoviev <[email protected]>
1 parent 655cf13 commit f87b0e2

File tree

5 files changed

+71
-32
lines changed

5 files changed

+71
-32
lines changed

Diff for: .vscode/launch.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@
5959
"args": ["src/__start.ts"],
6060
"env": {
6161
"ELASTIC_URL": "http://localhost:9200",
62-
"MONGO_URL": "postgresql://postgres:example@localhost:5432;mongodb://localhost:27017",
62+
"MONGO_URL": "mongodb://localhost:27017",
6363
"APM_SERVER_URL2": "http://localhost:8200",
6464
"METRICS_CONSOLE": "false",
6565
"METRICS_FILE": "${workspaceRoot}/metrics.txt", // Show metrics in console evert 30 seconds.,
66-
"STORAGE_CONFIG": "minio|localhost:9000?accessKey=minioadmin&secretKey=minioadmin",
66+
"STORAGE_CONFIG": "minio|localhost?accessKey=minioadmin&secretKey=minioadmin",
6767
"SERVER_SECRET": "secret",
6868
"ENABLE_CONSOLE": "true",
6969
"COLLABORATOR_URL": "ws://localhost:3078",

Diff for: packages/ui/src/components/PlainTextEditor.svelte

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
afterUpdate(adjustHeight)
4949
5050
function adjustHeight (): void {
51+
if (input == null) {
52+
return
53+
}
54+
5155
input.style.height = 'auto'
5256
input.style.height = `${input.scrollHeight + 2}px`
5357
}

Diff for: plugins/controlled-documents-resources/src/components/EditDocPanel.svelte

+40-22
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
9797
let innerWidth: number
9898
let isTitlePressed: boolean = false
99+
let creating: boolean = false
99100
100101
const notificationClient = getResource(notification.function.GetInboxNotificationsClient).then((res) => res())
101102
@@ -212,30 +213,41 @@
212213
}
213214
214215
async function onCreateNewDraft (): Promise<void> {
215-
if ($controlledDocument != null && $canCreateNewDraft && $documentLatestVersion != null) {
216-
const latest = $documentLatestVersion
217-
const version = { major: latest.major, minor: latest.minor + 1 }
218-
const project = await getLatestProjectId($controlledDocument.space)
219-
220-
if (project !== undefined) {
221-
try {
222-
const id = await createNewDraftForControlledDoc(
223-
client,
224-
$controlledDocument,
225-
$controlledDocument.space,
226-
version,
227-
project
228-
)
229-
const loc = getProjectDocumentLink(id, project)
230-
navigate(loc)
231-
} catch (err) {
232-
await setPlatformStatus(unknownError(err))
216+
if (creating) {
217+
return
218+
}
219+
220+
creating = true
221+
try {
222+
if ($controlledDocument != null && $canCreateNewDraft && $documentLatestVersion != null) {
223+
const latest = $documentLatestVersion
224+
const version = { major: latest.major, minor: latest.minor + 1 }
225+
const project = await getLatestProjectId($controlledDocument.space)
226+
227+
if (project !== undefined) {
228+
try {
229+
const { id, success } = await createNewDraftForControlledDoc(
230+
client,
231+
$controlledDocument,
232+
$controlledDocument.space,
233+
version,
234+
project
235+
)
236+
if (success) {
237+
const loc = getProjectDocumentLink(id, project)
238+
navigate(loc)
239+
}
240+
} catch (err) {
241+
await setPlatformStatus(unknownError(err))
242+
}
243+
} else {
244+
console.warn('No document project found for space', $controlledDocument.space)
233245
}
234246
} else {
235-
console.warn('No document project found for space', $controlledDocument.space)
247+
console.warn('Unexpected document state', $documentState)
236248
}
237-
} else {
238-
console.warn('Unexpected document state', $documentState)
249+
} finally {
250+
creating = false
239251
}
240252
}
241253
@@ -349,7 +361,13 @@
349361
{/if}
350362

351363
{#if $canCreateNewDraft}
352-
<Button label={documentRes.string.CreateNewDraft} kind="regular" on:click={onCreateNewDraft} />
364+
<Button
365+
label={documentRes.string.CreateNewDraft}
366+
kind="regular"
367+
loading={creating}
368+
disabled={creating}
369+
on:click={onCreateNewDraft}
370+
/>
353371
{:else if $canCreateNewSnapshot}
354372
<Button label={documentRes.string.EditDocument} kind="regular" on:click={onEditDocument} />
355373
{/if}

Diff for: plugins/controlled-documents-resources/src/docutils.ts

+19-7
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,21 @@ export async function createNewDraftForControlledDoc (
5050
version: { major: number, minor: number },
5151
project: Ref<Project>,
5252
newDraftDocId?: Ref<ControlledDocument>
53-
): Promise<Ref<ControlledDocument>> {
53+
): Promise<{ success: boolean, id: Ref<ControlledDocument> }> {
5454
const hierarchy = client.getHierarchy()
5555

5656
newDraftDocId = newDraftDocId ?? generateId()
5757

58+
const ops = client.apply(document.code)
59+
60+
const notMatchQuery = {
61+
...(document.template != null ? { template: document.template } : { template: { $exists: false } }),
62+
seqNumber: document.seqNumber,
63+
state: DocumentState.Draft
64+
}
65+
66+
ops.notMatch(documents.class.Document, notMatchQuery)
67+
5868
const collaborativeDoc = getCollaborativeDocForDocument(
5969
`DOC-${document.prefix}`,
6070
document.seqNumber,
@@ -76,7 +86,7 @@ export async function createNewDraftForControlledDoc (
7686
impactedDocuments: []
7787
}
7888

79-
await createChangeControl(client, newCCId, newCCSpec, document.space)
89+
await createChangeControl(ops, newCCId, newCCSpec, document.space)
8090

8191
// TODO: copy labels?
8292
const docSpec: AttachedData<ControlledDocument> = {
@@ -110,7 +120,7 @@ export async function createNewDraftForControlledDoc (
110120
})
111121

112122
if (meta !== undefined) {
113-
await client.addCollection(documents.class.ProjectDocument, meta.space, meta._id, meta._class, 'documents', {
123+
await ops.addCollection(documents.class.ProjectDocument, meta.space, meta._id, meta._class, 'documents', {
114124
project,
115125
initial: project,
116126
document: newDraftDocId
@@ -119,7 +129,7 @@ export async function createNewDraftForControlledDoc (
119129
console.error('project meta not found', project)
120130
}
121131

122-
await client.addCollection(
132+
await ops.addCollection(
123133
document._class,
124134
space,
125135
document.attachedTo,
@@ -131,7 +141,7 @@ export async function createNewDraftForControlledDoc (
131141

132142
if (hierarchy.hasMixin(document, documents.mixin.DocumentTemplate)) {
133143
const template = hierarchy.as<Document, DocumentTemplate>(document, documents.mixin.DocumentTemplate)
134-
await client.updateMixin(newDraftDocId, documents.class.Document, space, documents.mixin.DocumentTemplate, {
144+
await ops.updateMixin(newDraftDocId, documents.class.Document, space, documents.mixin.DocumentTemplate, {
135145
sequence: template.sequence,
136146
docPrefix: template.docPrefix
137147
})
@@ -143,7 +153,7 @@ export async function createNewDraftForControlledDoc (
143153
if (newDraftDoc === undefined) {
144154
console.error(`Document #${newDraftDocId} not found`)
145155
} else {
146-
await createDocumentTraining(client, newDraftDoc, {
156+
await createDocumentTraining(ops, newDraftDoc, {
147157
enabled: false,
148158
roles: documentTraining.roles,
149159
training: documentTraining.training,
@@ -154,7 +164,9 @@ export async function createNewDraftForControlledDoc (
154164
}
155165
}
156166

157-
return newDraftDocId
167+
const res = await ops.commit()
168+
169+
return { success: res.result, id: newDraftDocId }
158170
}
159171

160172
export async function createDocumentSnapshotAndEdit (client: TxOperations, document: ControlledDocument): Promise<void> {

Diff for: server/middleware/src/applyTx.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,12 @@ export class ApplyTxMiddleware extends BaseMiddleware implements Middleware {
9797
return { passed: true, onEnd: () => {} }
9898
}
9999
// Wait for synchronized.
100-
;(await this.scopes.get(applyIf.scope)) ?? Promise.resolve()
100+
const scopePromise = this.scopes.get(applyIf.scope)
101+
102+
if (scopePromise != null) {
103+
await scopePromise
104+
}
105+
101106
let onEnd = (): void => {}
102107
// Put sync code
103108
this.scopes.set(

0 commit comments

Comments
 (0)