1
1
import { Validator } from 'jsonschema'
2
+ import mongoose from 'mongoose'
2
3
3
4
import authentication from '../connectors/authentication/index.js'
4
5
import { ModelAction , ModelActionKeys } from '../connectors/authorisation/actions.js'
@@ -10,8 +11,9 @@ import { UserInterface } from '../models/User.js'
10
11
import { GetModelCardVersionOptions , GetModelCardVersionOptionsKeys , GetModelFiltersKeys } from '../types/enums.js'
11
12
import { isValidatorResultError } from '../types/ValidatorResultError.js'
12
13
import { toEntity } from '../utils/entity.js'
13
- import { BadReq , Forbidden , NotFound } from '../utils/error.js'
14
+ import { BadReq , Forbidden , InternalError , NotFound } from '../utils/error.js'
14
15
import { convertStringToId } from '../utils/id.js'
16
+ import log from './log.js'
15
17
import { findSchemaById } from './schema.js'
16
18
17
19
export type CreateModelParams = Pick < ModelInterface , 'name' | 'teamId' | 'description' | 'visibility' >
@@ -221,9 +223,17 @@ export async function _setModelCard(
221
223
}
222
224
223
225
const revision = new ModelCardRevisionModel ( { ...newDocument , modelId, createdBy : user . dn } )
224
- await revision . save ( )
225
226
226
- await ModelModel . updateOne ( { id : modelId } , { $set : { card : newDocument } } )
227
+ const message = 'Unable to save model card revision'
228
+ await mongoose . connection
229
+ . transaction ( async function executeUpdate ( session ) {
230
+ await revision . save ( { session } )
231
+ await ModelModel . updateOne ( { id : modelId } , { $set : { card : newDocument } } , { session : session } )
232
+ } )
233
+ . catch ( ( error ) => {
234
+ log . error ( 'Error when updating model card/revision. Transaction rolled back.' , error )
235
+ throw InternalError ( message , { modelId } )
236
+ } )
227
237
228
238
return revision
229
239
}
0 commit comments