feat: allow deleting standalone assets and debts
This commit is contained in:
1 parent
3edf611833
commit
c0ad193c73
8 files changed
+212
-40
No files matched your search
@@ -155,7 +155,7 @@ test('real REST account deletion preserves paired balances, blocks schedules, ca
|
||||
);
|
||||
assert.equal(
|
||||
(await call(a, '/positions/' + debt.data.id, 'DELETE', { confirmation: '关联债务' })).status,
|
||||
400,
|
||||
200,
|
||||
);
|
||||
} finally {
|
||||
await db.user.deleteMany({ where: { id: { in: users } } });
|
||||
@@ -247,3 +247,140 @@ test('draft review resolves only referenced visible names and canonical standalo
|
||||
await db.$disconnect();
|
||||
}
|
||||
});
|
||||
|
||||
test('standalone asset and both debt sides delete history and links; paired debts preserve account balances', async () => {
|
||||
const db = new PrismaClient();
|
||||
let userId = '';
|
||||
try {
|
||||
const u = await db.user.create({
|
||||
data: { username: 'delete_position_' + randomUUID(), passwordHash: 'unused', idleMinutes: 0 },
|
||||
});
|
||||
userId = u.id;
|
||||
const token = randomBytes(32).toString('hex'),
|
||||
sid = createHash('sha256').update(token).digest('hex');
|
||||
await db.session.create({
|
||||
data: { id: sid, userId, expiresAt: new Date(Date.now() + 3600000) },
|
||||
});
|
||||
async function call(path: string, method = 'GET', data?: unknown) {
|
||||
const r = await fetch(base + path, {
|
||||
method,
|
||||
headers: {
|
||||
Cookie: 'wp_session=' + token,
|
||||
Origin: origin,
|
||||
...(data ? { 'Content-Type': 'application/json' } : {}),
|
||||
},
|
||||
body: data ? JSON.stringify(data) : undefined,
|
||||
});
|
||||
return { status: r.status, data: await r.json() };
|
||||
}
|
||||
async function create(
|
||||
name: string,
|
||||
kind: string,
|
||||
side: string,
|
||||
amount: string,
|
||||
hidden = false,
|
||||
) {
|
||||
const r = await call('/positions', 'POST', {
|
||||
name,
|
||||
kind,
|
||||
side,
|
||||
amount,
|
||||
hidden,
|
||||
currency: 'CNY',
|
||||
category: 'other',
|
||||
date: today(),
|
||||
});
|
||||
assert.equal(r.status, 201);
|
||||
return r.data.id as string;
|
||||
}
|
||||
const cash = await create('关联测试账户', 'account', 'asset', '100');
|
||||
const asset = await create('独立资产删除测试', 'asset', 'asset', '42');
|
||||
const owed = await create('欠款删除测试', 'debt', 'liability', '10');
|
||||
const lent = await create('借出删除测试', 'debt', 'asset', '5');
|
||||
assert.equal(
|
||||
(await call('/positions/' + owed + '/links', 'PUT', { targetIds: [asset, cash] })).status,
|
||||
200,
|
||||
);
|
||||
assert.equal((await call('/positions/' + asset + '/deletion')).data.linkCount, 1);
|
||||
assert.equal(
|
||||
(await call('/positions/' + asset, 'DELETE', { confirmation: '独立资产删除测试' })).status,
|
||||
200,
|
||||
);
|
||||
assert.equal(await db.revision.count({ where: { positionId: asset } }), 0);
|
||||
assert.equal(await db.positionLink.count({ where: { targetId: asset } }), 0);
|
||||
assert.equal((await call('/positions/' + owed)).data.amount, '10');
|
||||
assert.equal((await call('/overview')).data.assets, '105.00');
|
||||
assert.equal(
|
||||
(await call('/positions/' + lent, 'DELETE', { confirmation: '借出删除测试' })).status,
|
||||
200,
|
||||
);
|
||||
assert.equal((await call('/overview')).data.assets, '100.00');
|
||||
const makeMovement = async (operation: string, amount: string) => {
|
||||
const r = await call('/transfers', 'POST', {
|
||||
operation,
|
||||
sourceId: cash,
|
||||
targetId: owed,
|
||||
amount,
|
||||
received: amount,
|
||||
fee: '0',
|
||||
date: today(),
|
||||
requestId: randomUUID(),
|
||||
});
|
||||
assert.equal(r.status, 201);
|
||||
return r.data.id as string;
|
||||
};
|
||||
const borrowed = await makeMovement('borrow', '20');
|
||||
const repayment = await makeMovement('repay', '5');
|
||||
assert.equal((await call('/positions/' + owed + '/deletion')).data.movementCount, 2);
|
||||
assert.equal(
|
||||
(await call('/positions/' + owed, 'DELETE', { confirmation: '欠款删除测试' })).status,
|
||||
409,
|
||||
);
|
||||
assert.equal((await call('/positions/' + cash)).data.amount, '115');
|
||||
assert.equal((await call('/positions/' + owed)).data.amount, '25');
|
||||
assert.equal((await call('/transfers/' + repayment, 'DELETE')).status, 200);
|
||||
assert.equal((await call('/transfers/' + borrowed, 'DELETE')).status, 200);
|
||||
assert.equal((await call('/positions/' + cash)).data.amount, '100');
|
||||
assert.equal(
|
||||
(await call('/positions/' + owed, 'DELETE', { confirmation: '欠款删除测试' })).status,
|
||||
200,
|
||||
);
|
||||
assert.equal(await db.revision.count({ where: { positionId: owed } }), 0);
|
||||
assert.equal(await db.positionLink.count({ where: { sourceId: owed } }), 0);
|
||||
assert.equal((await call('/overview')).data.liabilities, '0.00');
|
||||
assert.ok(
|
||||
!(await call('/history')).data.items.some((h: any) =>
|
||||
[asset, lent, owed].includes(h.positionId),
|
||||
),
|
||||
);
|
||||
assert.ok(
|
||||
!(await call('/calendar/day?date=' + today())).data.items.some((h: any) =>
|
||||
[asset, lent, owed].includes(h.positionId),
|
||||
),
|
||||
);
|
||||
for (const [kind, side] of [
|
||||
['asset', 'asset'],
|
||||
['debt', 'liability'],
|
||||
]) {
|
||||
const name = '隐藏归档删除-' + kind,
|
||||
id = await create(name, kind, side, '3', true);
|
||||
await db.position.update({ where: { id }, data: { archived: true } });
|
||||
assert.equal((await call('/positions/' + id + '/deletion')).status, 404);
|
||||
assert.equal((await call('/positions/' + id, 'DELETE', { confirmation: name })).status, 404);
|
||||
await db.session.update({
|
||||
where: { id: sid },
|
||||
data: { revealUntil: new Date(Date.now() + 600000) },
|
||||
});
|
||||
assert.equal((await call('/positions/' + id + '/deletion')).data.canDelete, true);
|
||||
assert.equal(
|
||||
(await call('/positions/' + id, 'DELETE', { confirmation: '错误名称' })).status,
|
||||
400,
|
||||
);
|
||||
assert.equal((await call('/positions/' + id, 'DELETE', { confirmation: name })).status, 200);
|
||||
await db.session.update({ where: { id: sid }, data: { revealUntil: null } });
|
||||
}
|
||||
} finally {
|
||||
if (userId) await db.user.deleteMany({ where: { id: userId } });
|
||||
await db.$disconnect();
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user