lrf %!s(int64=2) %!d(string=hai) anos
pai
achega
1ebd223c2a
Modificáronse 1 ficheiros con 9 adicións e 1 borrados
  1. 9 1
      app/service/user/address.js

+ 9 - 1
app/service/user/address.js

@@ -9,13 +9,21 @@ class AddressService extends CrudService {
   constructor(ctx) {
     super(ctx, 'address');
     this.model = this.ctx.model.User.Address;
+    // 默认值.为:非默认
+    this.defaultValue = '0';
+  }
+
+  async afterCreate(body, data) {
+    const { is_default } = data;
+    if (is_default !== this.defaultValue) await this.toDefault({ id: data._id });
+    return data;
   }
 
   async toDefault({ id }) {
     const data = await this.model.findById(id);
     if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
     const { customer } = data;
-    await this.model.updateMany({ customer }, { is_default: '0' });
+    await this.model.updateMany({ customer }, { is_default: this.defaultValue });
     data.is_default = '1';
     await data.save();
   }