Update house-keeping

This commit is contained in:
Luke Vella 2025-03-27 16:27:25 +00:00
parent 7efd8fc287
commit 3dcf958cd9
No known key found for this signature in database
GPG key ID: 469CAD687F0D784C
2 changed files with 103 additions and 41 deletions

View file

@ -12,19 +12,38 @@ import { checkApiAuthorization } from "@/utils/api-auth";
export async function POST() { export async function POST() {
const unauthorized = checkApiAuthorization(); const unauthorized = checkApiAuthorization();
if (unauthorized) return unauthorized; if (unauthorized) return unauthorized;
const polls = await prisma.poll.findMany({
where: {
deleted: false,
options: {
every: {
startTime: {
lt: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000),
},
},
},
OR: [
{ userId: null },
{
user: {
OR: [{ subscription: null }, { subscription: { active: false } }],
},
},
],
},
});
// Mark inactive polls as deleted in a single query // Mark inactive polls as deleted in a single query
const { count: markedDeleted } = await prisma.poll.updateMany({ const { count: markedDeleted } = await prisma.poll.updateMany({
where: { where: {
deleted: false, deleted: false,
options: { options: {
none: { every: {
startTime: { startTime: {
lt: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000), lt: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000),
}, },
}, },
}, },
// Include polls without a user or with users that don't have an active subscription
OR: [ OR: [
{ userId: null }, { userId: null },
{ {
@ -44,6 +63,7 @@ export async function POST() {
success: true, success: true,
summary: { summary: {
markedDeleted, markedDeleted,
polls,
}, },
}); });
} }

View file

@ -5,7 +5,7 @@ import dayjs from "dayjs";
/** /**
* This test suite tests the house-keeping API endpoints: * This test suite tests the house-keeping API endpoints:
* 1. delete-inactive-polls: Marks inactive polls as deleted * 1. delete-inactive-polls: Marks inactive polls as deleted
* 2. remove-deleted-polls: Permanently removes polls that have been marked as deleted for more than 7 days * 2. remove-deleted-polls: Permanently removes polls that have been marked as deleted for more than 30 days
*/ */
test.describe("House-keeping API", () => { test.describe("House-keeping API", () => {
// Store created poll IDs for cleanup // Store created poll IDs for cleanup
@ -15,11 +15,6 @@ test.describe("House-keeping API", () => {
// API Secret for authentication // API Secret for authentication
const API_SECRET = process.env.API_SECRET; const API_SECRET = process.env.API_SECRET;
test.beforeAll(async () => {
// Clean up any existing test data
await cleanup();
});
test.afterAll(async () => { test.afterAll(async () => {
// Clean up test data // Clean up test data
await cleanup(); await cleanup();
@ -96,6 +91,18 @@ test.describe("House-keeping API", () => {
participantUrlId: "old-poll-regular-user-participant", participantUrlId: "old-poll-regular-user-participant",
adminUrlId: "old-poll-regular-user-admin", adminUrlId: "old-poll-regular-user-admin",
userId: regularUser.id, userId: regularUser.id,
options: {
create: [
{
startTime: dayjs().subtract(35, "day").toDate(), // 35 days in the past
duration: 60,
},
{
startTime: dayjs().subtract(40, "day").toDate(), // 40 days in the past
duration: 60,
},
],
},
}, },
}); });
createdPollIds.push(oldPollRegularUser.id); createdPollIds.push(oldPollRegularUser.id);
@ -108,21 +115,46 @@ test.describe("House-keeping API", () => {
participantUrlId: "old-poll-pro-user-participant", participantUrlId: "old-poll-pro-user-participant",
adminUrlId: "old-poll-pro-user-admin", adminUrlId: "old-poll-pro-user-admin",
userId: proUser.id, userId: proUser.id,
options: {
create: [
{
startTime: dayjs().subtract(35, "day").toDate(), // 35 days in the past
duration: 60,
},
{
startTime: dayjs().subtract(40, "day").toDate(), // 40 days in the past
duration: 60,
},
],
},
}, },
}); });
createdPollIds.push(oldPollProUser.id); createdPollIds.push(oldPollProUser.id);
// 3. Recent poll from regular user (should NOT be marked as deleted) // 3. Old poll from guest user (should be marked as deleted)
const recentPollRegularUser = await prisma.poll.create({ const oldPollGuestUser = await prisma.poll.create({
data: { data: {
id: "recent-poll-regular-user", id: "old-poll-guest-user",
title: "Recent Poll Regular User", title: "Old Poll Guest User",
participantUrlId: "recent-poll-regular-user-participant", participantUrlId: "old-poll-guest-user-participant",
adminUrlId: "recent-poll-regular-user-admin", adminUrlId: "old-poll-guest-user-admin",
userId: regularUser.id, userId: null,
guestId: "guest-1",
options: {
create: [
{
startTime: dayjs().subtract(35, "day").toDate(), // 35 days in the past
duration: 60,
},
{
startTime: dayjs().subtract(40, "day").toDate(), // 40 days in the past
duration: 60,
},
],
},
}, },
}); });
createdPollIds.push(recentPollRegularUser.id); createdPollIds.push(oldPollGuestUser.id);
// 4. Old poll with future options from regular user (should NOT be marked as deleted) // 4. Old poll with future options from regular user (should NOT be marked as deleted)
const oldPollWithFutureOptions = await prisma.poll.create({ const oldPollWithFutureOptions = await prisma.poll.create({
@ -142,16 +174,29 @@ test.describe("House-keeping API", () => {
}); });
createdPollIds.push(oldPollWithFutureOptions.id); createdPollIds.push(oldPollWithFutureOptions.id);
// 5. Old poll without a user (should be marked as deleted) // 4. Poll with some dates less than 30 days in the past (should NOT be marked as deleted)
const oldPollNoUser = await prisma.poll.create({ const pollWithRecentPastDates = await prisma.poll.create({
data: { data: {
id: "old-poll-no-user", id: "poll-with-recent-past-dates",
title: "Old Poll No User", title: "Poll With Recent Past Dates",
participantUrlId: "old-poll-no-user-participant", participantUrlId: "poll-with-recent-past-dates-participant",
adminUrlId: "old-poll-no-user-admin", adminUrlId: "poll-with-recent-past-dates-admin",
userId: regularUser.id,
options: {
create: [
{
startTime: dayjs().subtract(15, "day").toDate(), // 15 days in the past
duration: 60,
},
{
startTime: dayjs().subtract(35, "day").toDate(), // 35 days in the past
duration: 60,
},
],
},
}, },
}); });
createdPollIds.push(oldPollNoUser.id); createdPollIds.push(pollWithRecentPastDates.id);
// Call the delete-inactive-polls endpoint // Call the delete-inactive-polls endpoint
const response = await request.post( const response = await request.post(
@ -167,9 +212,6 @@ test.describe("House-keeping API", () => {
const responseData = await response.json(); const responseData = await response.json();
expect(responseData.success).toBeTruthy(); expect(responseData.success).toBeTruthy();
// We expect 2 polls to be marked as deleted:
// - Old poll from regular user
// - Old poll without a user
expect(responseData.summary.markedDeleted).toBe(2); expect(responseData.summary.markedDeleted).toBe(2);
// Verify the state of each poll // Verify the state of each poll
@ -185,30 +227,30 @@ test.describe("House-keeping API", () => {
expect(updatedOldPollProUser?.deleted).toBe(false); expect(updatedOldPollProUser?.deleted).toBe(false);
expect(updatedOldPollProUser?.deletedAt).toBeNull(); expect(updatedOldPollProUser?.deletedAt).toBeNull();
const updatedRecentPollRegularUser = await prisma.poll.findUnique({
where: { id: recentPollRegularUser.id },
});
expect(updatedRecentPollRegularUser?.deleted).toBe(false);
expect(updatedRecentPollRegularUser?.deletedAt).toBeNull();
const updatedOldPollWithFutureOptions = await prisma.poll.findUnique({ const updatedOldPollWithFutureOptions = await prisma.poll.findUnique({
where: { id: oldPollWithFutureOptions.id }, where: { id: oldPollWithFutureOptions.id },
}); });
expect(updatedOldPollWithFutureOptions?.deleted).toBe(false); expect(updatedOldPollWithFutureOptions?.deleted).toBe(false);
expect(updatedOldPollWithFutureOptions?.deletedAt).toBeNull(); expect(updatedOldPollWithFutureOptions?.deletedAt).toBeNull();
const updatedOldPollNoUser = await prisma.poll.findUnique({ const updatedOldPollGuestUser = await prisma.poll.findUnique({
where: { id: oldPollNoUser.id }, where: { id: oldPollGuestUser.id },
}); });
expect(updatedOldPollNoUser?.deleted).toBe(true); expect(updatedOldPollGuestUser?.deleted).toBe(true);
expect(updatedOldPollNoUser?.deletedAt).not.toBeNull(); expect(updatedOldPollGuestUser?.deletedAt).not.toBeNull();
const updatedPollWithRecentPastDates = await prisma.poll.findUnique({
where: { id: pollWithRecentPastDates.id },
});
expect(updatedPollWithRecentPastDates?.deleted).toBe(false);
expect(updatedPollWithRecentPastDates?.deletedAt).toBeNull();
}); });
test("should permanently remove polls that have been marked as deleted for more than 7 days", async ({ test("should permanently remove polls that have been marked as deleted for more than 30 days", async ({
request, request,
baseURL, baseURL,
}) => { }) => {
// Create a poll that was marked as deleted more than 7 days ago // Create a poll that was marked as deleted more than 30 days ago
const oldDeletedPoll = await prisma.poll.create({ const oldDeletedPoll = await prisma.poll.create({
data: { data: {
id: "old-deleted-poll", id: "old-deleted-poll",
@ -216,12 +258,12 @@ test.describe("House-keeping API", () => {
participantUrlId: "old-deleted-poll-participant", participantUrlId: "old-deleted-poll-participant",
adminUrlId: "old-deleted-poll-admin", adminUrlId: "old-deleted-poll-admin",
deleted: true, deleted: true,
deletedAt: dayjs().subtract(8, "day").toDate(), // Deleted 8 days ago deletedAt: dayjs().subtract(31, "day").toDate(), // Deleted 31 days ago
}, },
}); });
createdPollIds.push(oldDeletedPoll.id); createdPollIds.push(oldDeletedPoll.id);
// Create a poll that was marked as deleted less than 7 days ago // Create a poll that was marked as deleted less than 30 days ago
const recentDeletedPoll = await prisma.poll.create({ const recentDeletedPoll = await prisma.poll.create({
data: { data: {
id: "recent-deleted-poll", id: "recent-deleted-poll",
@ -229,7 +271,7 @@ test.describe("House-keeping API", () => {
participantUrlId: "recent-deleted-poll-participant", participantUrlId: "recent-deleted-poll-participant",
adminUrlId: "recent-deleted-poll-admin", adminUrlId: "recent-deleted-poll-admin",
deleted: true, deleted: true,
deletedAt: dayjs().subtract(3, "day").toDate(), // Deleted 3 days ago deletedAt: dayjs().subtract(15, "day").toDate(), // Deleted 15 days ago
}, },
}); });
createdPollIds.push(recentDeletedPoll.id); createdPollIds.push(recentDeletedPoll.id);