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

+ 12 - 0
app/service/user/address.js

@@ -16,6 +16,7 @@ class AddressService extends CrudService {
   async afterCreate(body, data) {
     const { is_default } = data;
     if (is_default !== this.defaultValue) await this.toDefault({ id: data._id });
+    await this.checkDefault();
     return data;
   }
   /**
@@ -36,6 +37,17 @@ class AddressService extends CrudService {
     await this.model.updateMany({ customer }, { is_default: this.defaultValue });
     await this.model.updateOne({ _id: id }, { is_default: '1' });
   }
+  /**
+   * 确认用户有默认地址
+   * @param {Object} params 参数
+   * @param params.customer 用户id
+   */
+  async checkDefault({ customer }) {
+    const num = await this.model.count({ customer, is_default: '1' });
+    if (num > 0) return;
+    const data = await this.model.findOne({ customer });
+    if (data) await this.model.updateOne({ _id: data._id }, { is_default: '1' });
+  }
 }
 
 module.exports = AddressService;