Created
June 30, 2022 10:15
-
-
Save almeida1492/b5a7feeb13eb6eee6eff9bf2560b87ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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