add api route to check for expired videos

This commit is contained in:
MarconLP 2023-04-16 01:01:50 +02:00
parent 141c3e385c
commit 4c64aeeb35
No known key found for this signature in database
GPG key ID: A08A9C8B623F5EA5

View file

@ -0,0 +1,58 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { prisma } from "~/server/db";
import { DeleteObjectCommand } from "@aws-sdk/client-s3";
import { env } from "~/env.mjs";
import { s3 } from "~/server/aws/s3";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const expiredVideos = await prisma.video.findMany({
where: {
shareLinkExpiresAt: {
lte: new Date(),
},
delete_after_link_expires: true,
sharing: true,
},
include: {
user: true,
},
});
const updatedVideos = await prisma.video.updateMany({
where: {
shareLinkExpiresAt: {
lte: new Date(),
},
delete_after_link_expires: false,
sharing: true,
},
data: {
sharing: false,
},
});
const expiredVideoIds = expiredVideos.map((x) => x.id);
expiredVideos.map(async (video) => {
const deleteObjectCommand = new DeleteObjectCommand({
Bucket: env.AWS_BUCKET_NAME,
Key: video.user.id + "/" + video.id,
});
return await s3.send(deleteObjectCommand);
});
const deletedVideos = await prisma.video.deleteMany({
where: {
id: {
in: expiredVideoIds,
},
},
});
res
.status(200)
.json({ name: "John Doe", expiredVideos, updatedVideos, deletedVideos });
}