@@ -168,57 +168,58 @@ class PostCreator extends ModelCreator {
168168 // creating a code error
169169 this . creatingCodeError = true ;
170170 } else if ( this . attributes . errorGuid ) { // New style where we just associate NR error via error entity guid
171- this . request . log ( "errorGuid in request - looking up existing post" ) ;
172- let existingPost = await this . data . posts . getOneByQuery (
173- { errorGuid : this . attributes . errorGuid , deactivated : false } ,
174- { hint : PostIndexes . byErrorGuid }
171+ this . request . log ( 'errorGuid in request - looking up existing post' ) ;
172+ let existingPost = await this . data . posts . getOneByQuery (
173+ { errorGuid : this . attributes . errorGuid , deactivated : false } ,
174+ { hint : PostIndexes . byErrorGuid }
175+ ) ;
176+ if ( ! existingPost ) {
177+ // Check for a legacy codeError
178+ this . request . log ( 'looking for a legacy codeError' ) ;
179+ const legacyCodeError = await this . data . codeErrors . getOneByQuery (
180+ { objectId : this . attributes . errorGuid , deactivated : false } ,
181+ { hint : CodeErrorIndexes . byObjectId }
175182 ) ;
176- if ( ! existingPost ) {
177- // Check for a legacy codeError
178- this . request . log ( "looking for a legacy codeError" ) ;
179- const legacyCodeError = await this . data . codeErrors . getOneByQuery (
180- { objectId : this . attributes . errorGuid , deactivated : false } ,
181- { hint : CodeErrorIndexes . byObjectId }
182- ) ;
183- if ( legacyCodeError ) {
184- this . request . log ( "found a legacy codeError" ) ;
185- // need to update the post with the errorGuid and not create a stream (suppressSave will get set later based on existingPost)
186- const legacyPostId = legacyCodeError . get ( 'postId' ) ;
187- const legacyPost = await this . data . posts . getById ( legacyPostId ) ;
188- if ( legacyPost . get ( 'deactivated' ) !== true ) {
189- legacyPost . attributes . errorGuid = this . attributes . errorGuid ;
190- // legacyPost.set('errorGuid', this.attributes.errorGuid);
191- const op = {
192- $set : {
193- errorGuid : this . attributes . errorGuid ,
194- modifiedAt : Date . now ( )
195- }
196- } ;
197- this . transforms . postUpdate = await new ModelSaver ( {
198- request : this . request ,
199- collection : this . data . posts ,
200- id : legacyPostId
201- } ) . save ( op ) ;
202- this . request . log (
203- `updated post ${ legacyPostId } with errorGuid ${ this . attributes . errorGuid } for legacy codeError ${ legacyCodeError . get ( 'id' ) } `
204- ) ;
205- existingPost = legacyPost ;
206- } else {
207- this . request . log ( `skipping deactivated legacy post ${ legacyPostId } ` ) ;
208- }
183+ if ( legacyCodeError ) {
184+ this . request . log ( 'found a legacy codeError' ) ;
185+ // need to update the post with the errorGuid and not create a stream (suppressSave will get set later based on existingPost)
186+ const legacyPostId = legacyCodeError . get ( 'postId' ) ;
187+ const legacyPost = await this . data . posts . getById ( legacyPostId ) ;
188+ if ( legacyPost . get ( 'deactivated' ) !== true ) {
189+ legacyPost . attributes . errorGuid = this . attributes . errorGuid ;
190+ // legacyPost.set('errorGuid', this.attributes.errorGuid);
191+ const op = {
192+ $set : {
193+ errorGuid : this . attributes . errorGuid ,
194+ modifiedAt : Date . now ( )
195+ }
196+ } ;
197+ this . transforms . postUpdate = await new ModelSaver ( {
198+ request : this . request ,
199+ collection : this . data . posts ,
200+ id : legacyPostId
201+ } ) . save ( op ) ;
202+ this . request . log (
203+ `updated post ${ legacyPostId } with errorGuid ${ this . attributes . errorGuid } for legacy codeError ${ legacyCodeError . get ( 'id' ) } `
204+ ) ;
205+ existingPost = legacyPost ;
206+ } else {
207+ this . request . log ( `skipping deactivated legacy post ${ legacyPostId } ` ) ;
209208 }
210209 }
211- if ( existingPost ) {
212- this . request . log ( `errorGuid in request - found post, suppressing save, attributes: ${ JSON . stringify ( existingPost . attributes ) } ` ) ;
213- this . suppressSave = true ;
214- this . creatingCodeError = false ;
215- this . attributes = existingPost . attributes ;
216- } else {
217- this . request . log ( `errorGuid in request - did not find post, creating stream with accountId ${ this . attributes . accountId } ` ) ;
218- this . creatingCodeError = true ;
219- await this . createStream ( ) ;
220- }
221- } else if ( this . attributes . parentPostId ) {
210+ }
211+ if ( existingPost ) {
212+ this . request . log ( `errorGuid in request - found post, suppressing save, attributes: ${ JSON . stringify ( existingPost . attributes ) } ` ) ;
213+ this . suppressSave = true ;
214+ this . useId = existingPost . id ;
215+ this . creatingCodeError = false ;
216+ this . attributes = existingPost . attributes ;
217+ } else {
218+ this . request . log ( `errorGuid in request - did not find post, creating stream with accountId ${ this . attributes . accountId } ` ) ;
219+ this . creatingCodeError = true ;
220+ await this . createStream ( ) ;
221+ }
222+ } else if ( this . attributes . parentPostId ) {
222223 // is the parent a code error?
223224 this . parentPost = await this . data . posts . getById ( this . attributes . parentPostId ) ;
224225 if ( ! this . parentPost ) {
0 commit comments