Files
weiguang-jewelry-ledger/新账系统/new-shop.js
T

21 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict';
// 仅供没有账号和业务的新账本初始化;已有店铺通过配置和员工管理维护。
const Settings=require('./shop-settings');
function initialize(ledger,input,createUser){
if(ledger.one('SELECT id FROM users LIMIT 1')||ledger.one('SELECT id FROM orders LIMIT 1')||ledger.one('SELECT id FROM movements LIMIT 1'))throw new Error('已有账本不能重新初始化店铺');
const storeName=Settings.string(input.storeName,'店铺名称',80),label=Settings.string(input.ownerLabel,'店主显示名',30),loginName=Settings.string(input.ownerLoginName,'店主登录名',60,{login:true});
if(typeof input.ownerPassword!=='string'||input.ownerPassword.length<12||input.ownerPassword.length>500)throw new Error('店主密码须为12~500个字符');
ledger.db.exec('SAVEPOINT new_shop');
try{
// 空账本不强制创建两个助理,更不把其他店的姓名带入新店。
ledger.run("DELETE FROM team_members WHERE member<>'owner'");
createUser(ledger,loginName,'owner',input.ownerPassword,'owner');
const actor=ledger.one("SELECT id,name,role FROM users WHERE role='owner'");
ledger.setMeta('team-accounts-initialized-v1',{version:1});
const config=Settings.read(ledger);
Settings.update(ledger,{revision:config.revision,storeName,costPresets:Object.fromEntries(Object.keys(config.costPresets).map(key=>[key,null])),members:[{member:'owner',label,loginName}]},actor);
ledger.db.exec('RELEASE new_shop');return actor;
}catch(error){ledger.db.exec('ROLLBACK TO new_shop');ledger.db.exec('RELEASE new_shop');throw error;}
}
module.exports={initialize};