Skip to content

Commit 74a6798

Browse files
committed
improve(app-server): return free page on permanent deletion
1 parent 13bf3ce commit 74a6798

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

apps/app-server/src/trpc/api/pages/deletion/delete-permanently.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,46 @@ export async function deletePermanently({
5656
});
5757
}
5858

59-
// Delete page permanently
59+
// Check if page is free
60+
61+
let numFreePages;
6062

61-
await ctx.dataAbstraction.patch(
63+
const pageIsFree = await ctx.dataAbstraction.hget(
6264
'page',
6365
input.pageId,
64-
{ permanent_deletion_date: addDays(new Date(), -1) },
65-
{ dtrx },
66+
'free',
6667
);
6768

69+
if (pageIsFree) {
70+
numFreePages = await ctx.dataAbstraction.hget(
71+
'user',
72+
ctx.userId,
73+
'num-free-pages',
74+
);
75+
}
76+
77+
// Delete page permanently
78+
79+
await Promise.all([
80+
ctx.dataAbstraction.patch(
81+
'page',
82+
input.pageId,
83+
{ permanent_deletion_date: addDays(new Date(), -1) },
84+
{ dtrx },
85+
),
86+
87+
...(pageIsFree
88+
? [
89+
ctx.dataAbstraction.patch(
90+
'user',
91+
ctx.userId,
92+
{ num_free_pages: numFreePages + 1 },
93+
{ dtrx },
94+
),
95+
]
96+
: []),
97+
]);
98+
6899
checkRedlockSignalAborted(signals);
69100
});
70101
},

apps/app-server/src/trpc/api/pages/deletion/restore.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,28 @@ export async function restore({
4141

4242
// Check if page is deleted
4343

44+
const permanentDeletionDate = await ctx.dataAbstraction.hget(
45+
'page',
46+
input.pageId,
47+
'permanent-deletion-date',
48+
);
49+
50+
if (permanentDeletionDate == null) {
51+
throw new TRPCError({
52+
code: 'BAD_REQUEST',
53+
message: 'Page is not deleted.',
54+
});
55+
}
56+
57+
// Check if page is free and permanently deleted
58+
4459
if (
45-
(await ctx.dataAbstraction.hget(
46-
'page',
47-
input.pageId,
48-
'permanent-deletion-date',
49-
)) == null
60+
new Date() > permanentDeletionDate &&
61+
(await ctx.dataAbstraction.hget('page', input.pageId, 'free'))
5062
) {
5163
throw new TRPCError({
5264
code: 'BAD_REQUEST',
53-
message: 'Page is not deleted.',
65+
message: 'Cannot restore a permanently deleted free page.',
5466
});
5567
}
5668

0 commit comments

Comments
 (0)