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
@@ -160,7 +160,6 @@ export class PortfolioBusinessService {
|
||||
}
|
||||
async deletion(r: UserRequest, id: string) {
|
||||
const p = await this.own(r.userId, id, r.revealed);
|
||||
if (p.kind !== 'account') throw new BadRequestException('此功能仅用于删除账户');
|
||||
const [historyCount, movementCount, scheduleCount, linkCount] = await Promise.all([
|
||||
this.db.revision.count({ where: { positionId: id } }),
|
||||
this.db.transfer.count({
|
||||
@@ -192,23 +191,24 @@ export class PortfolioBusinessService {
|
||||
const p = await tx.position.findFirst({
|
||||
where: { id, userId: r.userId, ...(r.revealed ? {} : { hidden: false }) },
|
||||
});
|
||||
if (!p) throw new NotFoundException('账户不存在');
|
||||
if (p.kind !== 'account') throw new BadRequestException('此功能仅用于删除账户');
|
||||
if (!p) throw new NotFoundException('项目不存在');
|
||||
if (input.confirmation !== p.name)
|
||||
throw new BadRequestException('请输入完整账户名称确认删除');
|
||||
throw new BadRequestException('请输入完整项目名称确认删除');
|
||||
// Paired movements must be removed through replay, never via cascade.
|
||||
if (
|
||||
await tx.transfer.count({
|
||||
where: { userId: r.userId, OR: [{ sourceId: id }, { targetId: id }] },
|
||||
})
|
||||
)
|
||||
throw new ConflictException('账户存在资金往来,请先撤销相关记录,或选择归档保留历史');
|
||||
throw new ConflictException(
|
||||
'项目存在资金往来,请先撤销相关转账、借贷或还款记录,或选择归档保留历史',
|
||||
);
|
||||
if (
|
||||
await tx.schedule.count({
|
||||
where: { userId: r.userId, OR: [{ sourceId: id }, { targetId: id }] },
|
||||
})
|
||||
)
|
||||
throw new ConflictException('账户被定时计划使用,请先删除相关计划,或选择归档');
|
||||
throw new ConflictException('项目被定时计划使用,请先删除相关计划,或选择归档');
|
||||
await tx.position.delete({ where: { id } });
|
||||
return { ok: true };
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
});
|
||||
@@ -65,18 +65,12 @@ test('account deletion checks ownership and hidden visibility before deleting or
|
||||
await f.service.remove({ ...owner, revealed: true }, 'account', { confirmation: '测试账户' });
|
||||
assert.deepEqual(f.state(), { deleted: true, invalidated: true });
|
||||
});
|
||||
test('wrong confirmation, non-accounts, paired movements and schedules cannot cascade', async () => {
|
||||
test('wrong confirmation, paired movements and schedules cannot cascade', async () => {
|
||||
const f = fixture();
|
||||
await assert.rejects(
|
||||
() => f.service.remove(owner, 'account', { confirmation: '错误名称' }),
|
||||
BadRequestException,
|
||||
);
|
||||
f.row.kind = 'asset';
|
||||
await assert.rejects(
|
||||
() => f.service.remove(owner, 'account', { confirmation: '测试账户' }),
|
||||
BadRequestException,
|
||||
);
|
||||
f.row.kind = 'account';
|
||||
f.setMovements(1);
|
||||
await assert.rejects(
|
||||
() => f.service.remove(owner, 'account', { confirmation: '测试账户' }),
|
||||
@@ -95,3 +89,14 @@ test('wrong confirmation, non-accounts, paired movements and schedules cannot ca
|
||||
});
|
||||
assert.deepEqual(f.state(), { deleted: true, invalidated: true });
|
||||
});
|
||||
|
||||
test('standalone assets and debts support deletion and invalidate totals', async () => {
|
||||
for (const kind of ['asset', 'debt']) {
|
||||
const f = fixture();
|
||||
f.row.kind = kind;
|
||||
assert.deepEqual(await f.service.remove(owner, 'account', { confirmation: '测试账户' }), {
|
||||
ok: true,
|
||||
});
|
||||
assert.deepEqual(f.state(), { deleted: true, invalidated: true });
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user