@@ -311,6 +311,23 @@ func buildOrgUpdateAuditRecord(ctx context.Context, orgBeforeUpdate, orgAfterUpd
311311 return auditRecord
312312}
313313
314+ func buildOrgUpdateColumns (org organization.Organization ) (goqu.Record , error ) {
315+ record := goqu.Record {
316+ "title" : org .Title ,
317+ "avatar" : org .Avatar ,
318+ "updated_at" : goqu .L ("now()" ),
319+ }
320+ if org .Metadata == nil {
321+ return record , nil
322+ }
323+ marshaledMetadata , err := json .Marshal (org .Metadata )
324+ if err != nil {
325+ return nil , fmt .Errorf ("%w: %w" , errParse , err )
326+ }
327+ record ["metadata" ] = marshaledMetadata
328+ return record , nil
329+ }
330+
314331func (r OrganizationRepository ) UpdateByID (ctx context.Context , org organization.Organization ) (organization.Organization , error ) {
315332 if strings .TrimSpace (org .ID ) == "" {
316333 return organization.Organization {}, organization .ErrInvalidID
@@ -320,9 +337,9 @@ func (r OrganizationRepository) UpdateByID(ctx context.Context, org organization
320337 return organization.Organization {}, organization .ErrInvalidDetail
321338 }
322339
323- marshaledMetadata , err := json . Marshal (org . Metadata )
340+ updateRecord , err := buildOrgUpdateColumns (org )
324341 if err != nil {
325- return organization.Organization {}, fmt . Errorf ( "%w: %w" , errParse , err )
342+ return organization.Organization {}, err
326343 }
327344
328345 // Query to fetch org title before update
@@ -333,20 +350,14 @@ func (r OrganizationRepository) UpdateByID(ctx context.Context, org organization
333350 return organization.Organization {}, fmt .Errorf ("%w: %w" , errQuery , err )
334351 }
335352
336- query , params , err := dialect .Update (TABLE_ORGANIZATIONS ).Set (
337- goqu.Record {
338- "title" : org .Title ,
339- "avatar" : org .Avatar ,
340- "metadata" : marshaledMetadata ,
341- "updated_at" : goqu .L ("now()" ),
342- }).Where (goqu.Ex {
353+ query , params , err := dialect .Update (TABLE_ORGANIZATIONS ).Set (updateRecord ).Where (goqu.Ex {
343354 "id" : org .ID ,
344355 }, live (TABLE_ORGANIZATIONS )).Returning (& Organization {}).ToSQL ()
345356 if err != nil {
346357 return organization.Organization {}, fmt .Errorf ("%w: %w" , errQuery , err )
347358 }
348359
349- var orgModel Organization
360+ var updatedOrg organization. Organization
350361 if err = r .dbc .WithTxn (ctx , sql.TxOptions {}, func (tx * sqlx.Tx ) error {
351362 return r .dbc .WithTimeout (ctx , TABLE_ORGANIZATIONS , "Update" , func (ctx context.Context ) error {
352363 // Fetch title before update
@@ -356,12 +367,18 @@ func (r OrganizationRepository) UpdateByID(ctx context.Context, org organization
356367 }
357368
358369 // Execute update
370+ var orgModel Organization
359371 if err := tx .QueryRowxContext (ctx , query , params ... ).StructScan (& orgModel ); err != nil {
360372 return err
361373 }
374+ transformed , err := orgModel .transformToOrg ()
375+ if err != nil {
376+ return fmt .Errorf ("%w: %w" , errParse , err )
377+ }
378+ updatedOrg = transformed
362379
363380 // Create audit record
364- auditRecord := buildOrgUpdateAuditRecord (ctx , orgBeforeUpdate , orgModel , org .Metadata )
381+ auditRecord := buildOrgUpdateAuditRecord (ctx , orgBeforeUpdate , orgModel , updatedOrg .Metadata )
365382 return InsertAuditRecordInTx (ctx , tx , auditRecord )
366383 })
367384 }); err != nil {
@@ -378,22 +395,17 @@ func (r OrganizationRepository) UpdateByID(ctx context.Context, org organization
378395 }
379396 }
380397
381- org , err = orgModel .transformToOrg ()
382- if err != nil {
383- return organization.Organization {}, fmt .Errorf ("%w: %w" , errParse , err )
384- }
385-
386- return org , nil
398+ return updatedOrg , nil
387399}
388400
389401func (r OrganizationRepository ) UpdateByName (ctx context.Context , org organization.Organization ) (organization.Organization , error ) {
390402 if strings .TrimSpace (org .Name ) == "" {
391403 return organization.Organization {}, organization .ErrInvalidID
392404 }
393405
394- marshaledMetadata , err := json . Marshal (org . Metadata )
406+ updateRecord , err := buildOrgUpdateColumns (org )
395407 if err != nil {
396- return organization.Organization {}, fmt . Errorf ( "%w: %w" , errParse , err )
408+ return organization.Organization {}, err
397409 }
398410
399411 // Query to fetch org data before update
@@ -404,21 +416,15 @@ func (r OrganizationRepository) UpdateByName(ctx context.Context, org organizati
404416 return organization.Organization {}, fmt .Errorf ("%w: %w" , errQuery , err )
405417 }
406418
407- query , params , err := dialect .Update (TABLE_ORGANIZATIONS ).Set (
408- goqu.Record {
409- "title" : org .Title ,
410- "avatar" : org .Avatar ,
411- "metadata" : marshaledMetadata ,
412- "updated_at" : goqu .L ("now()" ),
413- }).Where (
419+ query , params , err := dialect .Update (TABLE_ORGANIZATIONS ).Set (updateRecord ).Where (
414420 goqu.Ex {
415421 "name" : org .Name ,
416422 }, live (TABLE_ORGANIZATIONS )).Returning (& Organization {}).ToSQL ()
417423 if err != nil {
418424 return organization.Organization {}, fmt .Errorf ("%w: %w" , errQuery , err )
419425 }
420426
421- var orgModel Organization
427+ var updatedOrg organization. Organization
422428 if err = r .dbc .WithTxn (ctx , sql.TxOptions {}, func (tx * sqlx.Tx ) error {
423429 return r .dbc .WithTimeout (ctx , TABLE_ORGANIZATIONS , "UpdateByName" , func (ctx context.Context ) error {
424430 // Fetch title before update
@@ -428,11 +434,17 @@ func (r OrganizationRepository) UpdateByName(ctx context.Context, org organizati
428434 }
429435
430436 // Execute update
437+ var orgModel Organization
431438 if err := tx .QueryRowxContext (ctx , query , params ... ).StructScan (& orgModel ); err != nil {
432439 return err
433440 }
441+ transformed , err := orgModel .transformToOrg ()
442+ if err != nil {
443+ return fmt .Errorf ("%w: %w" , errParse , err )
444+ }
445+ updatedOrg = transformed
434446
435- auditRecord := buildOrgUpdateAuditRecord (ctx , orgBeforeUpdate , orgModel , org .Metadata )
447+ auditRecord := buildOrgUpdateAuditRecord (ctx , orgBeforeUpdate , orgModel , updatedOrg .Metadata )
436448 return InsertAuditRecordInTx (ctx , tx , auditRecord )
437449 })
438450 }); err != nil {
@@ -447,12 +459,7 @@ func (r OrganizationRepository) UpdateByName(ctx context.Context, org organizati
447459 }
448460 }
449461
450- org , err = orgModel .transformToOrg ()
451- if err != nil {
452- return organization.Organization {}, fmt .Errorf ("%w: %w" , errParse , err )
453- }
454-
455- return org , nil
462+ return updatedOrg , nil
456463}
457464
458465func (r OrganizationRepository ) SetState (ctx context.Context , id string , state organization.State ) error {
0 commit comments