Website/db/app/api/items/refresh.ts

35 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-05-06 21:05:02 -04:00
import { CronJob } from "quirrel/blitz"
import { PrismaClient, Item } from '@prisma/client';
import { RawItemData } from '../../core/dto/rawData/RawItemData';
import { propsToClassKey } from "@mui/styles";
export default CronJob(
"api/items/refresh", // the path of this API route
"@daily", // cron schedule (see https://crontab.guru)
async () => {
const itemsResponse = await fetch('https://dev.sp-tarkov.com/SPT-AKI/Server/raw/branch/development/project/assets/database/templates/items.json');
if (itemsResponse.status >= 300) {
throw Error(`Could not retrieve items from Gitea: code ${itemsResponse.status}`)
}
const items: { [id: string]: RawItemData } = await itemsResponse.json();
const prisma: PrismaClient = new PrismaClient();
const promises: any[] = []
Object.entries(items).forEach(
async ([key, value]) => {
const {props, ...data} = RawItemData.fromRawData(value).toItemData();
promises.push(await prisma.item.upsert({
create: {props, ...data},
update: data,
where: {
internalId: data.internalId
},
}))
}
);
await Promise.all(promises);
}
)