-
Notifications
You must be signed in to change notification settings - Fork 28
Fix cleanup job for unsubmitted forms #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
7257b55
0df10c6
9152fce
6373c95
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -29,49 +29,60 @@ import { update_job_status } from "./generic_scheduler"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const cleanup_unsubmitted_forms = async (job: JobScheduleQueue) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //Find forms that were created 7 days ago and have not been submitted | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const sevenDaysAgoPlusOneDay = new Date( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sevenDaysAgo.getTime() + 24 * 60 * 60 * 1000 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const expiredTokens = await prisma.publicFormsTokens.findMany({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| where: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| createdAt: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| gte: sevenDaysAgo, // greater than or equal to 7 days ago | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lt: sevenDaysAgoPlusOneDay, // but less than 7 days ago + 1 day | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| createdAt: { lt: sevenDaysAgo }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| select: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| token: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| entityId: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| productId: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const token of expiredTokens) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const relationship = await prisma.relationship.findFirst({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| where: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| product_id: token.productId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| status: "new", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (relationship) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await prisma.$transaction([ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Delete relationship | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prisma.relationship.delete({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| where: { id: relationship.id }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // // Delete the token | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prisma.publicFormsTokens.delete({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| where: { token: token.token }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Delete all corpus items associated with the entity | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prisma.new_corpus.deleteMany({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await prisma.$transaction(async (tx) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const relationships = await tx.relationship.findMany({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| where: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| entity_id: token.entityId || "", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| product_id: token.productId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| status: "new", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Delete the entity (company) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prisma.entity.delete({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| where: { id: token.entityId || "" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (relationships.length > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await tx.relationship.deleteMany({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| where: { id: { in: relationships.map((r) => r.id) } }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await tx.publicFormsTokens.delete({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| where: { token: token.token }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (token.entityId) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await tx.new_corpus.deleteMany({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| where: { entity_id: token.entityId }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await tx.entity.delete({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| where: { id: token.entityId }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+69
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add safety checks before deleting the entity Delete the entity only if it’s no longer referenced by other tokens or non-“new” relationships. Current code can remove an entity still in use. - if (token.entityId) {
-
- await tx.new_corpus.deleteMany({
- where: { entity_id: token.entityId },
- });
-
- await tx.entity.delete({
- where: { id: token.entityId },
- });
- }
+ if (token.entityId) {
+ const [otherTokens, nonNewRels] = await Promise.all([
+ tx.publicFormsTokens.count({
+ where: { entityId: token.entityId, token: { not: token.token } },
+ }),
+ tx.relationship.count({
+ where: { entity_id: token.entityId, status: { not: "new" } },
+ }),
+ ]);
+ if (otherTokens === 0 && nonNewRels === 0) {
+ await tx.new_corpus.deleteMany({ where: { entity_id: token.entityId } });
+ await tx.entity.delete({ where: { id: token.entityId } });
+ } else {
+ // Entity still referenced; skip entity removal.
+ }
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (innerError) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.error( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| `Failed cleanup for token ${token.token} (entity ${token.entityId}):`, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| innerError | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Over-broad relationship deletion (by product only) risks data loss
Filtering only by product_id and status: "new" can delete unrelated “new” relationships for other tokens/entities in the same product. Restrict by the specific entity (or token) you are cleaning.
Optional: log the delete count from deleteMany for observability.
📝 Committable suggestion
🤖 Prompt for AI Agents