路由表化:迁移资金/人数/报销与打印路由
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e082069f55
commit
900fc2f527
5 files changed
+279
-205
No files matched your search
+2
-204
@@ -29,86 +29,6 @@ function csvText(value) {
|
||||
safe = /^[\s\u0000-\u001f]*[=+\-@]|^[\u0000-\u001f]/.test(text) ? "'" + text : text;
|
||||
return '"' + safe.replace(/"/g, '""') + '"';
|
||||
}
|
||||
function peopleCountResponse(ledger, result, actor) {
|
||||
// 旧操作号会返回原缓存;仅投影当前权限,绝不改缓存或重新执行写入。
|
||||
const unavailable = () => fail('原人数回执不完整,请重新读取当前记录核对;原操作不会重复执行', 409);
|
||||
if (
|
||||
!result ||
|
||||
typeof result !== 'object' ||
|
||||
Array.isArray(result) ||
|
||||
Object.hasOwn(result, 'error') ||
|
||||
!/^\d{4}-\d{2}-\d{2}$/.test(result.date) ||
|
||||
!Number.isSafeInteger(result.revision) ||
|
||||
result.revision < 0
|
||||
)
|
||||
unavailable();
|
||||
const parts = ledger.peopleCountParts(actor).map(([key]) => key),
|
||||
filtered = { date: result.date, revision: result.revision };
|
||||
if (typeof result.previousDate === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(result.previousDate))
|
||||
filtered.previousDate = result.previousDate;
|
||||
if (result.mode === 'daily-total') filtered.mode = result.mode;
|
||||
const count = value => value === null || (Number.isSafeInteger(value) && value >= 0 && value <= 1000000);
|
||||
for (const part of parts)
|
||||
for (const suffix of ['Count', 'Change', 'ChangeReason'])
|
||||
if (Object.hasOwn(result, part + suffix)) {
|
||||
const value = result[part + suffix],
|
||||
valid =
|
||||
suffix === 'Count'
|
||||
? count(value)
|
||||
: suffix === 'Change'
|
||||
? value === null || (Number.isSafeInteger(value) && Math.abs(value) <= 1000000)
|
||||
: [null, 'missing-current', 'missing-previous', 'missing-both'].includes(value);
|
||||
if (!valid) unavailable();
|
||||
filtered[part + suffix] = value;
|
||||
}
|
||||
if (Object.hasOwn(result, 'previous')) {
|
||||
if (!result.previous || typeof result.previous !== 'object' || Array.isArray(result.previous)) unavailable();
|
||||
filtered.previous = {};
|
||||
for (const part of parts)
|
||||
if (Object.hasOwn(result.previous, part + 'Count')) {
|
||||
const value = result.previous[part + 'Count'];
|
||||
if (!count(value)) unavailable();
|
||||
filtered.previous[part + 'Count'] = value;
|
||||
}
|
||||
}
|
||||
if (Object.hasOwn(result, 'previousDates')) {
|
||||
if (!result.previousDates || typeof result.previousDates !== 'object' || Array.isArray(result.previousDates))
|
||||
unavailable();
|
||||
filtered.previousDates = {};
|
||||
for (const part of parts)
|
||||
if (Object.hasOwn(result.previousDates, part)) {
|
||||
const value = result.previousDates[part];
|
||||
if (
|
||||
value !== null &&
|
||||
(typeof value !== 'string' ||
|
||||
!/^\d{4}-\d{2}-\d{2}$/.test(value) ||
|
||||
!Number.isFinite(Date.parse(value + 'T00:00:00Z')) ||
|
||||
new Date(value + 'T00:00:00Z').toISOString().slice(0, 10) !== value ||
|
||||
value < '2000-01-01' ||
|
||||
value >= result.date)
|
||||
)
|
||||
unavailable();
|
||||
if (
|
||||
Object.hasOwn(filtered.previous || {}, part + 'Count') &&
|
||||
(value === null) !== (filtered.previous[part + 'Count'] === null)
|
||||
)
|
||||
unavailable();
|
||||
filtered.previousDates[part] = value;
|
||||
}
|
||||
}
|
||||
if (Object.hasOwn(result, 'memberCounts')) {
|
||||
if (!result.memberCounts || typeof result.memberCounts !== 'object' || Array.isArray(result.memberCounts))
|
||||
unavailable();
|
||||
filtered.memberCounts = {};
|
||||
for (const [, , member] of ledger.peopleCountParts(actor))
|
||||
if (Object.hasOwn(result.memberCounts, member)) {
|
||||
const value = result.memberCounts[member];
|
||||
if (!count(value)) unavailable();
|
||||
filtered.memberCounts[member] = value;
|
||||
}
|
||||
}
|
||||
return filtered;
|
||||
}
|
||||
|
||||
function createApp(options = {}) {
|
||||
const mode = options.mode || process.env.APP_MODE || 'trial';
|
||||
@@ -306,50 +226,6 @@ function createApp(options = {}) {
|
||||
rawLedger: ledger,
|
||||
app,
|
||||
});
|
||||
const printStatusMatch = route.match(/^\/api\/print-status\/(order|recovery)\/([a-f0-9-]{36})$/);
|
||||
if (printStatusMatch) {
|
||||
const [, kind, id] = printStatusMatch;
|
||||
PrintStatus.access(actorLedger, kind, id, actor);
|
||||
if (!isPost) return send(res, 200, PrintStatus.read(actorLedger, kind, id, actor));
|
||||
const body = await read(req);
|
||||
PrintStatus.access(actorLedger, kind, id, actor);
|
||||
const result = actorLedger.transact(
|
||||
req.headers['idempotency-key'],
|
||||
actor,
|
||||
{ route, body },
|
||||
() => PrintStatus.update(actorLedger, kind, id, body, actor),
|
||||
{ syncTeamFunds: false },
|
||||
);
|
||||
return send(res, 200, result);
|
||||
}
|
||||
if (route === '/api/team-funds' && !isPost) {
|
||||
owner(actor);
|
||||
return send(res, 200, actorLedger.teamFunds(Object.fromEntries(url.searchParams), actor));
|
||||
}
|
||||
if (['/api/private-operations', '/api/people-count', '/api/people-count-history'].includes(route)) owner(actor);
|
||||
if (route === '/api/private-operations' && !isPost)
|
||||
return send(
|
||||
res,
|
||||
200,
|
||||
require('./private-operations').report(actorLedger, Object.fromEntries(url.searchParams), actor),
|
||||
);
|
||||
if (route === '/api/private-operations/people-history' && !isPost)
|
||||
return send(
|
||||
res,
|
||||
200,
|
||||
require('./private-operations').peopleHistory(actorLedger, Object.fromEntries(url.searchParams), actor),
|
||||
);
|
||||
if (route === '/api/private-operations/people' && !isPost)
|
||||
return send(
|
||||
res,
|
||||
200,
|
||||
require('./private-operations').entry(actorLedger, Object.fromEntries(url.searchParams), actor),
|
||||
);
|
||||
if (route === '/api/team-profits' && !isPost) {
|
||||
owner(actor);
|
||||
return send(res, 200, actorLedger.teamProfits(Object.fromEntries(url.searchParams), actor));
|
||||
}
|
||||
if (['/api/team-funds/transfers', '/api/team-funds/transfer'].includes(route)) owner(actor);
|
||||
if (route === '/api/logout' && isPost) {
|
||||
const token = req.headers.cookie
|
||||
?.split(';')
|
||||
@@ -554,19 +430,8 @@ function createApp(options = {}) {
|
||||
if (templateMatch[2]) fail('地址不存在', 404);
|
||||
return send(res, 200, actorLedger.orderTemplate(templateMatch[1]));
|
||||
}
|
||||
if (route === '/api/people-count' && !isPost)
|
||||
return send(res, 200, actorLedger.peopleCount(url.searchParams.get('date') ?? today()));
|
||||
if (route === '/api/people-count-history' && !isPost)
|
||||
return send(res, 200, actorLedger.peopleCountHistory(Object.fromEntries(url.searchParams)));
|
||||
if (route === '/api/daily-funds' && !isPost)
|
||||
return send(res, 200, actorLedger.dailyFunds(Object.fromEntries(url.searchParams), actor));
|
||||
if (route === '/api/reimbursement-funding-options' && !isPost)
|
||||
return send(res, 200, actorLedger.reimbursementFundingOptions(Object.fromEntries(url.searchParams), actor));
|
||||
if (route === '/api/reimbursements' && !isPost)
|
||||
return send(res, 200, actorLedger.reimbursements(Object.fromEntries(url.searchParams), actor));
|
||||
if (route === '/api/returned-stock-lots' && !isPost)
|
||||
return send(res, 200, actorLedger.returnedStockLots(Object.fromEntries(url.searchParams), actor));
|
||||
const reimbursementMatch = route.match(/^\/api\/reimbursements\/([a-f0-9-]{36})\/(update|approve|reject)$/);
|
||||
if (route === '/api/paper-number' && !isPost)
|
||||
return send(res, 200, { number: actorLedger.paperNumber(url.searchParams.get('date') || today()) });
|
||||
const orderAfterSaleMatch = route.match(/^\/api\/orders\/([a-f0-9-]+)\/(after-sale|after-sale-preview)$/);
|
||||
@@ -588,58 +453,6 @@ function createApp(options = {}) {
|
||||
});
|
||||
fail('地址不存在', 404);
|
||||
}
|
||||
const recoveryPrintMatch = route.match(/^\/print\/recoveries\/([a-f0-9-]{36})$/);
|
||||
if (recoveryPrintMatch && !isPost) {
|
||||
const dto = actorLedger.publicRecovery(recoveryPrintMatch[1], actor),
|
||||
print = dto.mode === 'exchange' ? exchangeCustomerPrintDocument : recoveryCustomerPrintDocument;
|
||||
return send(
|
||||
res,
|
||||
200,
|
||||
PrintStatus.decorate(
|
||||
print(dto, ShopSettings.printingSettings(actorLedger)),
|
||||
actorLedger,
|
||||
'recovery',
|
||||
recoveryPrintMatch[1],
|
||||
actor,
|
||||
),
|
||||
'text/html; charset=utf-8',
|
||||
);
|
||||
}
|
||||
if (route.startsWith('/print/') && !isPost) {
|
||||
const id = route.slice(7),
|
||||
order = actorLedger.detail(id);
|
||||
if (order.recovery_id) {
|
||||
const dto = actorLedger.publicRecovery(order.recovery_id, actor);
|
||||
return send(
|
||||
res,
|
||||
200,
|
||||
PrintStatus.decorate(
|
||||
exchangeCustomerPrintDocument(dto, ShopSettings.printingSettings(actorLedger)),
|
||||
actorLedger,
|
||||
'order',
|
||||
id,
|
||||
actor,
|
||||
),
|
||||
'text/html; charset=utf-8',
|
||||
);
|
||||
}
|
||||
return send(
|
||||
res,
|
||||
200,
|
||||
PrintStatus.decorate(
|
||||
receipt(
|
||||
actorLedger.publicOrder(id),
|
||||
ShopSettings.printingSettings(actorLedger),
|
||||
url.searchParams.get('mode'),
|
||||
),
|
||||
actorLedger,
|
||||
'order',
|
||||
id,
|
||||
actor,
|
||||
),
|
||||
'text/html; charset=utf-8',
|
||||
);
|
||||
}
|
||||
if (route === '/api/report' && !isPost) {
|
||||
const report = actorLedger.reportFor(Object.fromEntries(url.searchParams), actor);
|
||||
if (actor.role !== 'owner') report.peopleCount = null;
|
||||
@@ -721,18 +534,6 @@ function createApp(options = {}) {
|
||||
return actorLedger.approveRecoveryChange(recoveryChangeMatch[1], b, actor);
|
||||
if (recoveryChangeMatch?.[2] === 'reject')
|
||||
return actorLedger.rejectRecoveryChange(recoveryChangeMatch[1], b, actor);
|
||||
if (['/api/team-funds/transfers', '/api/team-funds/transfer'].includes(route))
|
||||
return actorLedger.transferTeamFunds(b, actor);
|
||||
if (route === '/api/reimbursements') return actorLedger.createReimbursement(b, actor);
|
||||
if (route === '/api/daily-funds/confirm') return actorLedger.confirmDailyOutlay(b, actor);
|
||||
if (reimbursementMatch) {
|
||||
const [, id, action] = reimbursementMatch;
|
||||
return action === 'update'
|
||||
? actorLedger.updateReimbursement(id, b, actor)
|
||||
: action === 'approve'
|
||||
? actorLedger.approveReimbursement(id, b, actor)
|
||||
: actorLedger.rejectReimbursement(id, b, actor);
|
||||
}
|
||||
if (route === '/api/order-templates') return actorLedger.saveOrderTemplate(null, b, actor);
|
||||
if (templateMatch && templateMatch[2] === 'update')
|
||||
return actorLedger.saveOrderTemplate(templateMatch[1], b, actor);
|
||||
@@ -794,9 +595,6 @@ function createApp(options = {}) {
|
||||
return actorLedger.rejectOrderCancellation(cancellationMatch[1], b, actor);
|
||||
if (match && match[2] === 'cancellation') return actorLedger.requestOrderCancellation(match[1], b, actor);
|
||||
if (match && match[2] === 'canceltoday') return actorLedger.cancelOrderToday(match[1], b, actor);
|
||||
if (route === '/api/private-operations/people')
|
||||
return require('./private-operations').save(actorLedger, b, actor);
|
||||
if (route === '/api/people-count') return actorLedger.savePeopleCount(b, actor);
|
||||
if (match && match[2] === 'edit')
|
||||
return actorLedger.editOrder(match[1], OrderNoteTags.submission(actorLedger, b, actor, match[1]), actor);
|
||||
if (match && match[2]) return actorLedger.action(match[1], match[2], b, actor);
|
||||
@@ -824,10 +622,10 @@ function createApp(options = {}) {
|
||||
fail('地址不存在', 404);
|
||||
},
|
||||
{
|
||||
syncTeamFunds: !['/api/private-operations/people', '/api/password'].includes(route),
|
||||
syncTeamFunds: !['/api/password'].includes(route),
|
||||
},
|
||||
);
|
||||
return send(res, 200, route === '/api/people-count' ? peopleCountResponse(actorLedger, result, actor) : result);
|
||||
return send(res, 200, result);
|
||||
}
|
||||
const assets = {
|
||||
'/': 'index.html',
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
'use strict';
|
||||
const { fail } = require('../ledger');
|
||||
function peopleCountResponse(ledger, result, actor) {
|
||||
// 旧操作号会返回原缓存;仅投影当前权限,绝不改缓存或重新执行写入。
|
||||
const unavailable = () => fail('原人数回执不完整,请重新读取当前记录核对;原操作不会重复执行', 409);
|
||||
if (
|
||||
!result ||
|
||||
typeof result !== 'object' ||
|
||||
Array.isArray(result) ||
|
||||
Object.hasOwn(result, 'error') ||
|
||||
!/^\d{4}-\d{2}-\d{2}$/.test(result.date) ||
|
||||
!Number.isSafeInteger(result.revision) ||
|
||||
result.revision < 0
|
||||
)
|
||||
unavailable();
|
||||
const parts = ledger.peopleCountParts(actor).map(([key]) => key),
|
||||
filtered = { date: result.date, revision: result.revision };
|
||||
if (typeof result.previousDate === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(result.previousDate))
|
||||
filtered.previousDate = result.previousDate;
|
||||
if (result.mode === 'daily-total') filtered.mode = result.mode;
|
||||
const count = value => value === null || (Number.isSafeInteger(value) && value >= 0 && value <= 1000000);
|
||||
for (const part of parts)
|
||||
for (const suffix of ['Count', 'Change', 'ChangeReason'])
|
||||
if (Object.hasOwn(result, part + suffix)) {
|
||||
const value = result[part + suffix],
|
||||
valid =
|
||||
suffix === 'Count'
|
||||
? count(value)
|
||||
: suffix === 'Change'
|
||||
? value === null || (Number.isSafeInteger(value) && Math.abs(value) <= 1000000)
|
||||
: [null, 'missing-current', 'missing-previous', 'missing-both'].includes(value);
|
||||
if (!valid) unavailable();
|
||||
filtered[part + suffix] = value;
|
||||
}
|
||||
if (Object.hasOwn(result, 'previous')) {
|
||||
if (!result.previous || typeof result.previous !== 'object' || Array.isArray(result.previous)) unavailable();
|
||||
filtered.previous = {};
|
||||
for (const part of parts)
|
||||
if (Object.hasOwn(result.previous, part + 'Count')) {
|
||||
const value = result.previous[part + 'Count'];
|
||||
if (!count(value)) unavailable();
|
||||
filtered.previous[part + 'Count'] = value;
|
||||
}
|
||||
}
|
||||
if (Object.hasOwn(result, 'previousDates')) {
|
||||
if (!result.previousDates || typeof result.previousDates !== 'object' || Array.isArray(result.previousDates))
|
||||
unavailable();
|
||||
filtered.previousDates = {};
|
||||
for (const part of parts)
|
||||
if (Object.hasOwn(result.previousDates, part)) {
|
||||
const value = result.previousDates[part];
|
||||
if (
|
||||
value !== null &&
|
||||
(typeof value !== 'string' ||
|
||||
!/^\d{4}-\d{2}-\d{2}$/.test(value) ||
|
||||
!Number.isFinite(Date.parse(value + 'T00:00:00Z')) ||
|
||||
new Date(value + 'T00:00:00Z').toISOString().slice(0, 10) !== value ||
|
||||
value < '2000-01-01' ||
|
||||
value >= result.date)
|
||||
)
|
||||
unavailable();
|
||||
if (
|
||||
Object.hasOwn(filtered.previous || {}, part + 'Count') &&
|
||||
(value === null) !== (filtered.previous[part + 'Count'] === null)
|
||||
)
|
||||
unavailable();
|
||||
filtered.previousDates[part] = value;
|
||||
}
|
||||
}
|
||||
if (Object.hasOwn(result, 'memberCounts')) {
|
||||
if (!result.memberCounts || typeof result.memberCounts !== 'object' || Array.isArray(result.memberCounts))
|
||||
unavailable();
|
||||
filtered.memberCounts = {};
|
||||
for (const [, , member] of ledger.peopleCountParts(actor))
|
||||
if (Object.hasOwn(result.memberCounts, member)) {
|
||||
const value = result.memberCounts[member];
|
||||
if (!count(value)) unavailable();
|
||||
filtered.memberCounts[member] = value;
|
||||
}
|
||||
}
|
||||
return filtered;
|
||||
}
|
||||
module.exports = { peopleCountResponse };
|
||||
@@ -0,0 +1,118 @@
|
||||
'use strict';
|
||||
// 团队资金、团队利润、私账经营与人数、每日资金、报销。
|
||||
const PrivateOperations = require('../../private-operations');
|
||||
const { today } = require('../../ledger');
|
||||
const { notFound } = require('../http');
|
||||
const { peopleCountResponse } = require('../people-count-response');
|
||||
|
||||
const TRANSFER_PATHS = ['/api/team-funds/transfers', '/api/team-funds/transfer'];
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/api/team-funds',
|
||||
role: 'owner',
|
||||
handler: ({ ledger, query, actor }) => ledger.teamFunds(query, actor),
|
||||
},
|
||||
...TRANSFER_PATHS.flatMap(path => [
|
||||
// 旧代码对转账地址不分请求方式先校验店主,GET 随后 404。
|
||||
{ method: 'GET', path, role: 'owner', handler: notFound },
|
||||
{
|
||||
method: 'POST',
|
||||
path,
|
||||
role: 'owner',
|
||||
write: true,
|
||||
handler: ({ ledger, body, actor }) => ledger.transferTeamFunds(body, actor),
|
||||
},
|
||||
]),
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/api/team-profits',
|
||||
role: 'owner',
|
||||
handler: ({ ledger, query, actor }) => ledger.teamProfits(query, actor),
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/api/private-operations',
|
||||
role: 'owner',
|
||||
handler: ({ ledger, query, actor }) => PrivateOperations.report(ledger, query, actor),
|
||||
},
|
||||
// 旧代码对这两个地址不分请求方式先校验店主;POST 随后照常读请求体、进入事务再 404。
|
||||
{ method: 'POST', path: '/api/private-operations', role: 'owner', write: true, handler: notFound },
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/api/private-operations/people-history',
|
||||
handler: ({ ledger, query, actor }) => PrivateOperations.peopleHistory(ledger, query, actor),
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/api/private-operations/people',
|
||||
handler: ({ ledger, query, actor }) => PrivateOperations.entry(ledger, query, actor),
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
path: '/api/private-operations/people',
|
||||
write: true,
|
||||
syncTeamFunds: false,
|
||||
handler: ({ ledger, body, actor }) => PrivateOperations.save(ledger, body, actor),
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/api/people-count',
|
||||
role: 'owner',
|
||||
handler: ({ ledger, url }) => ledger.peopleCount(url.searchParams.get('date') ?? today()),
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
path: '/api/people-count',
|
||||
role: 'owner',
|
||||
write: true,
|
||||
handler: ({ ledger, body, actor }) => ledger.savePeopleCount(body, actor),
|
||||
project: ({ ledger, actor }, result) => peopleCountResponse(ledger, result, actor),
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/api/people-count-history',
|
||||
role: 'owner',
|
||||
handler: ({ ledger, query }) => ledger.peopleCountHistory(query),
|
||||
},
|
||||
{ method: 'POST', path: '/api/people-count-history', role: 'owner', write: true, handler: notFound },
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/api/daily-funds',
|
||||
handler: ({ ledger, query, actor }) => ledger.dailyFunds(query, actor),
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
path: '/api/daily-funds/confirm',
|
||||
write: true,
|
||||
handler: ({ ledger, body, actor }) => ledger.confirmDailyOutlay(body, actor),
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/api/reimbursement-funding-options',
|
||||
handler: ({ ledger, query, actor }) => ledger.reimbursementFundingOptions(query, actor),
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/api/reimbursements',
|
||||
handler: ({ ledger, query, actor }) => ledger.reimbursements(query, actor),
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
path: '/api/reimbursements',
|
||||
write: true,
|
||||
handler: ({ ledger, body, actor }) => ledger.createReimbursement(body, actor),
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
path: '/api/reimbursements/:id(uuid)/:action(update|approve|reject)',
|
||||
write: true,
|
||||
handler: ({ ledger, params: { id, action }, body, actor }) =>
|
||||
action === 'update'
|
||||
? ledger.updateReimbursement(id, body, actor)
|
||||
: action === 'approve'
|
||||
? ledger.approveReimbursement(id, body, actor)
|
||||
: ledger.rejectReimbursement(id, body, actor),
|
||||
},
|
||||
];
|
||||
@@ -1,3 +1,3 @@
|
||||
'use strict';
|
||||
// 全部路由按声明顺序匹配;同一请求方式下路径互有重叠的路由都在同一文件内按先具体后通配排列。
|
||||
module.exports = [...require('./shop.routes')];
|
||||
module.exports = [...require('./shop.routes'), ...require('./funds.routes'), ...require('./print.routes')];
|
||||
@@ -0,0 +1,75 @@
|
||||
'use strict';
|
||||
// 打印状态与打印页(HTML)。
|
||||
const PrintStatus = require('../../print-status');
|
||||
const ShopSettings = require('../../shop-settings');
|
||||
const { receipt } = require('../../printing');
|
||||
const { recoveryCustomerPrintDocument, exchangeCustomerPrintDocument } = require('../../recovery-printing');
|
||||
const { HTML } = require('../http');
|
||||
const { readBody } = require('../middleware/body');
|
||||
|
||||
const STATUS = '/api/print-status/:kind(order|recovery)/:id(uuid)';
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
method: 'GET',
|
||||
path: STATUS,
|
||||
handler: ({ ledger, params: { kind, id }, actor }) => {
|
||||
PrintStatus.access(ledger, kind, id, actor);
|
||||
return PrintStatus.read(ledger, kind, id, actor);
|
||||
},
|
||||
},
|
||||
{
|
||||
// 读请求体前后各核对一次访问权限;不复核会话;事务不同步团队资金。
|
||||
method: 'POST',
|
||||
path: STATUS,
|
||||
direct: true,
|
||||
handler: async ({ req, url, ledger, params: { kind, id }, actor }) => {
|
||||
PrintStatus.access(ledger, kind, id, actor);
|
||||
const body = await readBody(req);
|
||||
PrintStatus.access(ledger, kind, id, actor);
|
||||
return ledger.transact(
|
||||
req.headers['idempotency-key'],
|
||||
actor,
|
||||
{ route: url.pathname, body },
|
||||
() => PrintStatus.update(ledger, kind, id, body, actor),
|
||||
{ syncTeamFunds: false },
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
// 须在 /print/*id 之前。
|
||||
method: 'GET',
|
||||
path: '/print/recoveries/:id(uuid)',
|
||||
contentType: HTML,
|
||||
handler: ({ ledger, params: { id }, actor }) => {
|
||||
const dto = ledger.publicRecovery(id, actor),
|
||||
print = dto.mode === 'exchange' ? exchangeCustomerPrintDocument : recoveryCustomerPrintDocument;
|
||||
return PrintStatus.decorate(print(dto, ShopSettings.printingSettings(ledger)), ledger, 'recovery', id, actor);
|
||||
},
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/print/*id',
|
||||
contentType: HTML,
|
||||
handler: ({ ledger, url, params: { id }, actor }) => {
|
||||
const order = ledger.detail(id);
|
||||
if (order.recovery_id) {
|
||||
const dto = ledger.publicRecovery(order.recovery_id, actor);
|
||||
return PrintStatus.decorate(
|
||||
exchangeCustomerPrintDocument(dto, ShopSettings.printingSettings(ledger)),
|
||||
ledger,
|
||||
'order',
|
||||
id,
|
||||
actor,
|
||||
);
|
||||
}
|
||||
return PrintStatus.decorate(
|
||||
receipt(ledger.publicOrder(id), ShopSettings.printingSettings(ledger), url.searchParams.get('mode')),
|
||||
ledger,
|
||||
'order',
|
||||
id,
|
||||
actor,
|
||||
);
|
||||
},
|
||||
},
|
||||
];
|
||||
Reference in new issue
Block a user