lrf 2 tahun lalu
induk
melakukan
27af4b1361
1 mengubah file dengan 14 tambahan dan 0 penghapusan
  1. 14 0
      app/service/user/admin.js

+ 14 - 0
app/service/user/admin.js

@@ -10,6 +10,20 @@ class AdminService extends CrudService {
     super(ctx, 'admin');
     this.model = this.ctx.model.User.Admin;
   }
+
+  async beforeCreate(data) {
+    const { account } = data;
+    const num = await this.model.count({ account });
+    if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该账号名已存在');
+    return data;
+  }
+  async beforeUpdate(filter, update) {
+    const { account } = update;
+    const id = _.get(filter, '_id', _.get(filter, 'id'));
+    const num = await this.model.count({ _id: { $ne: id }, account });
+    if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该账号名已存在');
+    return { filter, update };
+  }
   /**
    * 登陆
    * @param {Object} body 登陆参数