-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAddFeatureChange.ts
More file actions
223 lines (204 loc) · 6.97 KB
/
AddFeatureChange.ts
File metadata and controls
223 lines (204 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* eslint-disable @typescript-eslint/restrict-template-expressions */
/* eslint-disable @typescript-eslint/require-await */
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
import {
type ChangeOptions,
type ClientDataStore,
FeatureChange,
type LocalGFF3DataStore,
type SerializedFeatureChange,
type ServerDataStore,
} from '@apollo-annotation/common'
import { type AnnotationFeatureSnapshot } from '@apollo-annotation/mst'
import { DeleteFeatureChange } from './DeleteFeatureChange'
interface SerializedAddFeatureChangeBase extends SerializedFeatureChange {
typeName: 'AddFeatureChange'
}
export interface AddFeatureChangeDetails {
addedFeature: AnnotationFeatureSnapshot
parentFeatureId?: string // Parent feature to where feature will be added
copyFeature?: boolean // Are we copying or adding a new child feature
allIds?: string[]
}
interface SerializedAddFeatureChangeSingle
extends SerializedAddFeatureChangeBase,
AddFeatureChangeDetails {}
interface SerializedAddFeatureChangeMultiple
extends SerializedAddFeatureChangeBase {
changes: AddFeatureChangeDetails[]
}
export type SerializedAddFeatureChange =
| SerializedAddFeatureChangeSingle
| SerializedAddFeatureChangeMultiple
export class AddFeatureChange extends FeatureChange {
typeName = 'AddFeatureChange' as const
changes: AddFeatureChangeDetails[]
constructor(json: SerializedAddFeatureChange, options?: ChangeOptions) {
super(json, options)
this.changes = 'changes' in json ? json.changes : [json]
}
// eslint-disable-next-line @typescript-eslint/class-literal-property-style
get notification() {
return 'Feature added successfully'
}
toJSON(): SerializedAddFeatureChange {
const { assembly, changedIds, changes, typeName } = this
if (changes.length === 1) {
const [{ addedFeature, allIds, copyFeature, parentFeatureId }] = changes
return {
typeName,
changedIds,
assembly,
addedFeature,
parentFeatureId,
copyFeature,
allIds,
}
}
return { typeName, changedIds, assembly, changes }
}
/**
* Applies the required change to database
* @param backend - parameters from backend
* @returns
*/
async executeOnServer(backend: ServerDataStore) {
const { assemblyModel, featureModel, refSeqModel, session, user } = backend
const { assembly, changes, logger } = this
const assemblyDoc = await assemblyModel
.findById(assembly)
.session(session)
.exec()
if (!assemblyDoc) {
const errMsg = `*** ERROR: Assembly with id "${assembly}" not found`
logger.error(errMsg)
throw new Error(errMsg)
}
let featureCnt = 0
logger.debug?.(`changes: ${JSON.stringify(changes)}`)
// Loop the changes
for (const change of changes) {
logger.debug?.(`change: ${JSON.stringify(change)}`)
const { addedFeature, allIds, copyFeature, parentFeatureId } = change
const { _id, refSeq } = addedFeature
const refSeqDoc = await refSeqModel
.findById(refSeq)
.session(session)
.exec()
if (!refSeqDoc) {
throw new Error(
`RefSeq was not found by assembly "${assembly}" and seq_id "${refSeq}" not found`,
)
}
// CopyFeature is called from CopyFeature.tsx
if (copyFeature) {
// Add into Mongo
const [newFeatureDoc] = await featureModel.create(
[{ ...addedFeature, allIds, status: -1, user }],
{ session },
)
logger.debug?.(
`Copied feature, docId "${newFeatureDoc._id}" to assembly "${assembly}"`,
)
featureCnt++
} else {
// Adding new child feature
if (parentFeatureId) {
const topLevelFeature = await featureModel
.findOne({ allIds: parentFeatureId })
.session(session)
.exec()
if (!topLevelFeature) {
throw new Error(
`Could not find feature with ID "${parentFeatureId}"`,
)
}
const parentFeature = this.getFeatureFromId(
topLevelFeature,
parentFeatureId,
)
if (!parentFeature) {
throw new Error(
`Could not find feature with ID "${parentFeatureId}" in feature "${topLevelFeature._id}"`,
)
}
this.addChild(parentFeature, addedFeature)
const childIds = this.getChildFeatureIds(addedFeature)
topLevelFeature.allIds.push(_id, ...childIds)
await topLevelFeature.save()
} else {
const childIds = this.getChildFeatureIds(addedFeature)
const allIdsV2 = [_id, ...childIds]
const [newFeatureDoc] = await featureModel.create(
[{ allIds: allIdsV2, status: 0, ...addedFeature }],
{ session },
)
logger.verbose?.(`Added docId "${newFeatureDoc._id}"`)
}
}
featureCnt++
}
logger.debug?.(`Added ${featureCnt} new feature(s) into database.`)
}
async executeOnLocalGFF3(_backend: LocalGFF3DataStore) {
throw new Error('executeOnLocalGFF3 not implemented')
}
async executeOnClient(dataStore: ClientDataStore) {
if (!dataStore) {
throw new Error('No data store')
}
const { assembly, changes } = this
for (const change of changes) {
const { addedFeature, parentFeatureId } = change
if (parentFeatureId) {
let parentFeature = dataStore.getFeature(parentFeatureId)
// maybe the parent feature hasn't been loaded yet
if (!parentFeature) {
await dataStore.loadFeatures([
{
assemblyName: assembly,
refName: addedFeature.refSeq,
start: addedFeature.min,
end: addedFeature.max,
},
])
parentFeature = dataStore.getFeature(parentFeatureId)
if (!parentFeature) {
throw new Error(
`Could not find parent feature "${parentFeatureId}"`,
)
}
}
// create an ID for the parent feature if it does not have one
if (!parentFeature.attributes.get('_id')) {
parentFeature.setAttribute('_id', [parentFeature._id])
}
parentFeature.addChild(addedFeature)
} else {
dataStore.addFeature(assembly, addedFeature)
}
}
}
getInverse() {
const { assembly, changedIds, changes, logger } = this
const inverseChangedIds = [...changedIds].reverse()
const inverseChanges = [...changes].reverse().map((addFeatureChange) => ({
deletedFeature: addFeatureChange.addedFeature,
parentFeatureId: addFeatureChange.parentFeatureId,
}))
return new DeleteFeatureChange(
{
changedIds: inverseChangedIds,
typeName: 'DeleteFeatureChange',
changes: inverseChanges,
assembly,
},
{ logger },
)
}
}
export function isAddFeatureChange(
change: unknown,
): change is AddFeatureChange {
return (change as AddFeatureChange).typeName === 'AddFeatureChange'
}