Skip to content

Commit bb01dee

Browse files
committed
feat: allow user to fork own documents
1 parent ad49180 commit bb01dee

File tree

1 file changed

+47
-41
lines changed

1 file changed

+47
-41
lines changed

libs/backend/src/services/document/document.service.ts

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -114,55 +114,61 @@ export class DocumentSerivce {
114114

115115
const document = await this.documentRepository.constructItem(dto)
116116

117-
if (mutation.authorId === loggedInAccountId) {
118-
// create document remotely, add id to mutation remotely ? (need to be merged)
117+
// ToDo: should mutation be saved locally or remote?
118+
// if (mutation.authorId === loggedInAccountId) {
119+
// // create document remotely, add id to mutation remotely ? (need to be merged)
120+
121+
// // can be null if mutation was locally edited before
122+
// const editingMutation = this._replaceAppInstance(mutation, appId, null, document.id)
123+
124+
// if (!editingMutation) {
125+
// throw new Error('No app in mutation with that ID and empty document')
126+
// }
127+
128+
// const [savedDocument, savedMutation] = await this.unitOfWorkService.runInTransaction((tx) =>
129+
// Promise.all([
130+
// this.documentRepository.createItem(document, tx),
131+
// this.mutationService.editMutation(
132+
// { ...editingMutation, source: EntitySourceType.Origin },
133+
// undefined,
134+
// tx
135+
// ), // ToDo: undefined
136+
// ])
137+
// )
138+
139+
// return {
140+
// document: savedDocument.toDto(),
141+
// mutation: savedMutation,
142+
// }
143+
// } else {
144+
// create document remotely, make mutation local, add id to mutation
145+
146+
const savedDocument = await this.documentRepository.createItem(document)
147+
148+
if (
149+
mutation.apps.some((app) => app.appId === appId && app.documentId === document.id) ||
150+
mutation.source === EntitySourceType.Origin
151+
) {
152+
// ToDo: navie implementation
153+
mutation.apps
154+
.filter((app) => app.documentId !== null) // remove apps without documents
155+
.concat({ appId, documentId: document.id }) // add new document
119156

120-
// can be null if mutation was locally edited before
121-
const editingMutation = this._replaceAppInstance(mutation, appId, null, document.id)
122-
123-
if (!editingMutation) {
124-
throw new Error('No app in mutation with that ID and empty document')
125-
}
126-
127-
const [savedDocument, savedMutation] = await this.unitOfWorkService.runInTransaction((tx) =>
128-
Promise.all([
129-
this.documentRepository.createItem(document, tx),
130-
this.mutationService.editMutation(
131-
{ ...editingMutation, source: EntitySourceType.Origin },
132-
undefined,
133-
tx
134-
), // ToDo: undefined
135-
])
136-
)
157+
const savedMutation = await this.mutationService.saveMutation({
158+
...mutation,
159+
source: EntitySourceType.Local,
160+
})
137161

138162
return {
139163
document: savedDocument.toDto(),
140164
mutation: savedMutation,
141165
}
142-
} else {
143-
// create document remotely, make mutation local, add id to mutation
144-
145-
const savedDocument = await this.documentRepository.createItem(document)
146-
147-
// ToDo: null authorId is possible here
148-
const editingMutation = this._replaceAppInstance(mutation, appId, null, document.id)
149-
150-
if (editingMutation || mutation.source === EntitySourceType.Origin) {
151-
const savedMutation = await this.mutationService.saveMutation({
152-
...(editingMutation ?? mutation),
153-
source: EntitySourceType.Local,
154-
})
155-
156-
return {
157-
document: savedDocument.toDto(),
158-
mutation: savedMutation,
159-
}
160-
}
166+
}
161167

162-
return {
163-
document: savedDocument.toDto(),
164-
}
168+
return {
169+
document: savedDocument.toDto(),
165170
}
171+
// }
166172
}
167173

168174
private async _editLocalDocumentInMutation(

0 commit comments

Comments
 (0)