UNPKG

rapid-ztx

Version:

Rapid ZTX module

93 lines (81 loc) 2.43 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.credit_cards) { throw new Error('解绑贷记卡错:用户未绑定贷记卡'); } let card_idx = -1; for (let i = 0; i < sumer.credit_cards.length; i++) { const card = sumer.credit_cards[i]; if (card.card_no == info.card_no) { card_idx = i; break; } } if (card_idx < 0) { throw new Error('解绑贷记卡错:卡号不匹配'); } // 从贷记卡列表中删除指定的记录 const unbind_cards = sumer.credit_cards.splice(card_idx, 1); // 更新用户贷记卡列表 await unique_db.none(` update rapid_ctod_sumers set credit_cards = $1 where uuid = $2 `, [ JSON.stringify(sumer.credit_cards), sumer.uuid ]); // 记录解绑历史 await unique_db.none(` insert into rapid_ctod_bhist ( uuid, sumer, action, card, notes ) values ( $1, $2, $3, $4, $5 ) `, [ uuid.v4(), sumer.uuid, 4, unbind_cards[0], '解绑贷记卡' ]); // 给代理商联系人发送邮件通知 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;