alipay.service.js 1.29 KB
const alipayRepository = require('./alipay.repository');
const { logger } = global.yoho;
const AlertAgent = require('../../utils/alert-manager');

/**
 * dingtalk 告警
 */
const AlertType = {
  MySQL: 0,
  Knex: 1,
};
const alipayAlert = new AlertAgent('alipay', {
  [AlertType.MySQL]: 'mysql error',
  [AlertAgent.Knex]: 'knex insert error'
}, ['1735697170', '15210602915']);

async function create({ uid, alipayAccount, alipayName }) {
  try {
    const result = await alipayRepository.insert({
      uid, alipayAccount, alipayName
    });

    if (!result) {
      alipayAlert.send(AlertType.Knex);
      return {
        error: {
          code: 500,
          message: '服务器出错,请稍候再来提交'
        }
      };
    }

    return {
      code: 200,
      message: '提交成功'
    };
  } catch (e) {
    if (e.code === 'ER_DUP_ENTRY') {
      // 重复的情况
      return {
        error: {
          code: 404,
          message: '您已提交过支付宝信息,请不要重复提交!'
        }
      };
    } else {
      logger.error('mysql error =>', e);
      alipayAlert.send(AlertType.MySQL, e.message);
      return {
        error: {
          code: 500,
          message: '服务器出错,请稍候再来提交'
        }
      };
    }
  }
}

module.exports = {
  create
};