Skip to content

Instantly share code, notes, and snippets.

@almeida1492
Created June 30, 2022 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save almeida1492/b5a7feeb13eb6eee6eff9bf2560b87ed to your computer and use it in GitHub Desktop.
Save almeida1492/b5a7feeb13eb6eee6eff9bf2560b87ed to your computer and use it in GitHub Desktop.
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
async function removeOldReports() {
const activeReports = await prisma.report.findMany({
where: { isActive: true },
});
const toDeactivateIdList = activeReports
.map((report) => {
const currentDate = new Date();
const creationDate = new Date(report.createdAt);
const _diff =
(currentDate.getTime() - creationDate.getTime()) / 1000 / 60;
const diff = Math.abs(Math.round(_diff));
if (diff > 20) {
return report.id;
}
})
.filter((id) => id);
toDeactivateIdList.forEach(async (id) => {
try {
const updatedReport = await prisma.report.update({
where: {
id,
},
data: { isActive: false },
});
console.dir({ id: updatedReport.id }, { depth: Infinity });
} catch (error) {
console.log(error);
}
});
console.log(
"⏱ ",
`Deactivating ${toDeactivateIdList.length} ${
toDeactivateIdList.length >= 2 ? "reports" : "report"
}.`
);
}
removeOldReports();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment