11import { isPlainObject } from 'lodash' ;
2- import { ObjectID } from 'mongodb' ;
2+ import { ObjectId , ReturnDocument } from 'mongodb' ;
33import IfMatch from '../errors/IfMatch' ;
44import IfNoneMatch from '../errors/IfNoneMatch' ;
55import MaxEtags from '../errors/MaxEtags' ;
@@ -23,8 +23,8 @@ export default (config: Config) => {
2323
2424 const profileFilter = {
2525 activityId : opts . activityId ,
26- lrs : new ObjectID ( opts . client . lrs_id ) ,
27- organisation : new ObjectID ( opts . client . organisation ) ,
26+ lrs : new ObjectId ( opts . client . lrs_id ) ,
27+ organisation : new ObjectId ( opts . client . organisation ) ,
2828 profileId : opts . profileId ,
2929 } ;
3030
@@ -56,39 +56,36 @@ export default (config: Config) => {
5656 $set : update ,
5757 } ,
5858 {
59- returnOriginal : false , // Ensures the updated document is returned.
59+ returnDocument : ReturnDocument . AFTER , // Ensures the updated document is returned.
6060 upsert : false , // Does not create the profile when it doesn't exist.
6161 } ,
6262 ) ;
6363
6464 // Determines if the Profile was updated.
6565 // Docs: https://docs.mongodb.com/manual/reference/command/getLastError/#getLastError.n
66- const updatedDocuments = updateOpResult . lastErrorObject . n as number ;
66+ const updatedDocuments = updateOpResult . lastErrorObject ? .n as number ;
6767 if ( updatedDocuments === 1 ) {
68+ const opResult = await collection . findOne ( { _id : updateOpResult . value ?. _id } ) ;
69+
6870 return {
69- extension : updateOpResult . value . extension ,
70- id : updateOpResult . value . _id . toString ( ) ,
71+ extension : opResult ? .extension ,
72+ id : opResult ?. _id . toString ( ) as string ,
7173 } ;
7274 }
7375 }
7476
75- // Creates the profile if it doesn't already exist.
76- // Docs: http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#findOneAndUpdate
77- // Docs: http://bit.ly/findAndModifyWriteOpResult
7877 const createOpResult = await collection . findOneAndUpdate (
7978 profileFilter ,
8079 {
8180 $setOnInsert : update ,
8281 } ,
8382 {
84- returnOriginal : false , // Ensures the updated document is returned.
85- upsert : true , // Creates the profile when it's not found.
83+ returnDocument : ReturnDocument . AFTER ,
84+ upsert : true ,
8685 } ,
8786 ) ;
8887
89- // Determines if the Profile was created or found.
90- // Docs: https://docs.mongodb.com/manual/reference/command/getLastError/#getLastError.n
91- const wasCreated = createOpResult . lastErrorObject . upserted !== undefined ;
88+ const wasCreated = ! createOpResult . lastErrorObject ?. updatedExisting ;
9289
9390 // Throws the IfMatch error when the profile already exists.
9491 // This is because there must have been an ETag mismatch in the previous update.
@@ -100,9 +97,13 @@ export default (config: Config) => {
10097 throw new IfNoneMatch ( ) ;
10198 }
10299
100+ const id = wasCreated ? createOpResult . lastErrorObject ?. upserted : createOpResult . value ?. _id ;
101+
102+ const opResult = await collection . findOne ( { _id : id } ) ;
103+
103104 return {
104- extension : createOpResult . value . extension ,
105- id : createOpResult . value . _id . toString ( ) ,
105+ extension : opResult ? .extension ,
106+ id : opResult ?. _id . toString ( ) as string ,
106107 } ;
107108 } ;
108109} ;
0 commit comments