UNPKG

rapid-ztx

Version:

Rapid ZTX module

96 lines (83 loc) 2.64 kB
/** * Copyright (c) 2017 Lucky Byte, Inc. */ const debug = require('debug')('ztx'); const uuid = require('uuid'); const rapid_db = require('rapid-db'); const utils = require('../lib/utils'); /** * 解绑借记卡 * * 参数: * info: 绑卡参数,包含下面的数据: * * sumer: 用户 uuid * * card_no: 借记卡卡号,用于鉴别 * * 返回: * 失败抛出异常 */ const unbind = async (info) => { const keys = [ 'sumer', 'card_no' ]; for (let i = 0; i < keys.length; i++) { const key = keys[i]; if (!info[key]) { throw new Error(`解绑借记卡错:参数缺少[${key}]`); } } // 查询用户信息 const sumer = await unique_db.oneOrNone(` select * from rapid_ctod_sumers where uuid = $1 `, [ info.sumer ]); if (!sumer) { throw new Error('解绑借记卡错:用户不存在'); } // 用户未绑借记卡情况下,不能解绑 if (!sumer.debit_card) { throw new Error('解绑借记卡错:用户未绑定借记卡'); } // 判断卡号是否一致 if (sumer.debit_card.card_no != info.card_no) { throw new Error('解绑借记卡错:卡号鉴别错'); } // 如果已经绑定了贷记卡,则不能解绑 if (sumer.credit_cards && sumer.credit_cards.length > 0) { throw new Error('解绑借记卡错:请先解绑已绑定的信用卡'); } // 解绑借记卡,更新时再次检查是否已绑定贷记卡 const result = await unique_db.one(` update rapid_ctod_sumers set debit_card = null where uuid = $1 and ( credit_cards is null or jsonb_array_length(credit_cards) = 0 ) returning debit_card `, [ sumer.uuid ]); // 如果没有正常更新为空,则返回错误 if (result.debit_card) { throw new Error('解绑借记卡错:更新数据库错'); } // 记录解绑历史 await unique_db.none(` insert into rapid_ctod_bhist ( uuid, sumer, action, card, notes ) values ( $1, $2, $3, $4, $5 ) `, [ uuid.v4(), sumer.uuid, 2, sumer.debit_card, '解绑借记卡' ]); // 给代理商联系人发送邮件通知 try { const html = ` <p>用户[${sumer.name}][${sumer.mobile}]解除借记卡绑定!</p> `; await utils.mail_to_agent(sumer.agent, '用户解除绑卡通知', html); } catch (err) { await rapid_db.notify.warn(`发送解绑卡通知邮件错: ${err.message}`); } } module.exports = unbind;