vendure icon indicating copy to clipboard operation
vendure copied to clipboard

Can not update User customFields programmatically to DB

Open roytan883 opened this issue 2 years ago • 2 comments

Describe the bug Can not update customFields programmatically

To Reproduce Steps to reproduce the behavior:

  1. config
  customFields: {
    Customer: [
      { name: 'rtsUid', type: 'string', public: true, readonly: false, internal: false, defaultValue: "", },
    ],
    User: [
      { name: 'rtsUid', type: 'string', public: true, readonly: false, internal: false, defaultValue: "", },
    ],
  },
  1. change it when auth
const user = await this.externalAuthenticationService.findCustomerUser(ctx, this.name, userInfo.uid);
if (user) {
    let ctxAdmin = await this.requestContextService.create({
        apiType: 'admin',
      })
      let dbUser = await this.connection
        .getRepository(ctxAdmin, User)
        .findOne(user.id);
      console.log("authenticate OK shop user find update rtsUid dbUser:", dbUser)
      if (_.has(dbUser, 'customFields') 
      // && _.isEmpty(_.get(dbUser, 'customFields.rtsUid'))
      ) {
        dbUser.customFields['rtsUid'] = userInfo.uid
        let dbUser2 = await this.connection.getRepository(ctxAdmin, User).save(dbUser);
        console.log("authenticate OK shop user find update rtsUid dbUser2:", dbUser2)
      }
}
  1. everything looks good(by log), but not save to DB
authenticate OK shop user find update rtsUid dbUser: User {
   createdAt: 2022-08-03T10:08:01.305Z,
  updatedAt: 2022-08-12T05:20:26.303Z,
 deletedAt: null,
  identifier: '[email protected]',
  verified: true,
  lastLogin: 2022-08-12T05:24:35.397Z,
  id: '29b40368-59a0-483f-a815-980e0e851d2e',
   customFields: CustomUserFields { rtsUid: '' }
 }
 authenticate OK shop user find update rtsUid dbUser2: User {
   createdAt: 2022-08-03T10:08:01.305Z,
 updatedAt: 2022-08-12T05:25:03.452Z,
   deletedAt: null,
  identifier: '[email protected]',
  verified: true,
   lastLogin: 2022-08-12T05:24:35.397Z,
  id: '29b40368-59a0-483f-a815-980e0e851d2e',
  customFields: CustomUserFields { rtsUid: '1588741115563_e8bb676202a0' }
 }

  1. manually check DB, that user's DB column customFieldsRtsuid still empty.

Expected behavior save to db

Environment (please complete the following information):

  • @vendure/core version: 1.6.4
  • Nodejs version
  • Database (mysql/postgres etc): postgres 14.4

Additional context Also I try those, all not work: 1, create 'ctxAdmin' by requestContextService 2, change readonly from false to true

I flow this https://www.vendure.io/docs/developer-guide/customizing-models/ but programmatically not work.

Update:

Then I try to do the same code on Customer, it works. It save to DB OK. So this bug only happen on User ???

roytan883 avatar Aug 12 '22 05:08 roytan883

Then I try to do the same code on Customer, it works. It save to DB OK. So this bug only happen on User ???

roytan883 avatar Aug 12 '22 05:08 roytan883

After I change get user by findCustomerUser not by .getRepository(ctxAdmin, User).findOne(user.id), it works. @michaelbromley user instance get from Service and getRepository are different ???

    const user = await this.externalAuthenticationService.findCustomerUser(ctx, this.name, userInfo.uid);
    if (user) {
      console.log("authenticate OK shop user find:", userInfo.loginName)

      let ctxAdmin = await this.requestContextService.create({
        apiType: 'admin',
      })
      let dbUser = user
      // let dbUser = await this.connection
      //   .getRepository(ctxAdmin, User)
      //   .findOne(user.id);
      if (_.has(dbUser, 'customFields') && _.isEmpty(_.get(dbUser, 'customFields.rtsUid'))) {
        console.log("authenticate OK shop user find update rtsUid dbUser:", dbUser)
        dbUser.customFields['rtsUid'] = userInfo.uid
        let dbUser2 = await this.connection.getRepository(ctxAdmin, User).save(dbUser);
        console.log("authenticate OK shop user find update rtsUid dbUser2:", dbUser2)
      }

roytan883 avatar Aug 12 '22 06:08 roytan883