Added failsafe in increment function that will create a new DailyStat document if the latest one is not the current day. Added a check in the cron function to check that it won't create a new document if the latest DailyStat is for the current date.
Some checks failed
Main / build-and-push-docker-image (20.x) (push) Failing after 4m14s
Some checks failed
Main / build-and-push-docker-image (20.x) (push) Failing after 4m14s
This commit is contained in:
@@ -4,6 +4,21 @@ import { BigIntResolver } from "graphql-scalars";
|
|||||||
const envMutationKey = process.env.MUTATION_KEY || "";
|
const envMutationKey = process.env.MUTATION_KEY || "";
|
||||||
const env = process.env.NODE_ENV || "development";
|
const env = process.env.NODE_ENV || "development";
|
||||||
|
|
||||||
|
const isDailyStatToday = (date: string): boolean => {
|
||||||
|
const today = new Date().setHours(0, 0, 0, 0);
|
||||||
|
const dailyStatCreated = new Date(date).setHours(0, 0, 0, 0);
|
||||||
|
|
||||||
|
return today == dailyStatCreated;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getLatestDailyStat = async () => {
|
||||||
|
return await prisma.dailyStats.findFirst({
|
||||||
|
orderBy: {
|
||||||
|
createdAt: "desc"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
// Prisma
|
// Prisma
|
||||||
export const resolvers = {
|
export const resolvers = {
|
||||||
// scalars
|
// scalars
|
||||||
@@ -39,7 +54,19 @@ export const resolvers = {
|
|||||||
orderBy: {
|
orderBy: {
|
||||||
createdAt: "asc"
|
createdAt: "asc"
|
||||||
}
|
}
|
||||||
})
|
}),
|
||||||
|
getGroupStats: async (
|
||||||
|
_parent: unknown,
|
||||||
|
data: { groupID: number }
|
||||||
|
// _ctx: unknown
|
||||||
|
) => {
|
||||||
|
const { groupID } = data;
|
||||||
|
return await prisma.groups.findUnique({
|
||||||
|
where: {
|
||||||
|
telegramID: groupID
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
},
|
},
|
||||||
Mutation: {
|
Mutation: {
|
||||||
init: async (
|
init: async (
|
||||||
@@ -89,6 +116,15 @@ export const resolvers = {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const latestDailyStats = await getLatestDailyStat();
|
||||||
|
|
||||||
|
if (latestDailyStats !== null) {
|
||||||
|
if (isDailyStatToday(String(latestDailyStats.createdAt))) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const date = new Date().toISOString();
|
const date = new Date().toISOString();
|
||||||
|
|
||||||
await prisma.dailyStats.create({ data: { createdAt: date } });
|
await prisma.dailyStats.create({ data: { createdAt: date } });
|
||||||
@@ -151,7 +187,7 @@ export const resolvers = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const existingGroup = await prisma.groups.findFirst({
|
const existingGroup = await prisma.groups.findUnique({
|
||||||
where: { telegramID: groupID }
|
where: { telegramID: groupID }
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -222,22 +258,32 @@ export const resolvers = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return await prisma.dailyStats
|
let latestDailyStats = await getLatestDailyStat();
|
||||||
.findFirst({
|
|
||||||
orderBy: {
|
if (latestDailyStats !== null) {
|
||||||
createdAt: "desc"
|
if (!isDailyStatToday(String(latestDailyStats.createdAt))) {
|
||||||
|
const date = new Date().toISOString();
|
||||||
|
|
||||||
|
await prisma.dailyStats
|
||||||
|
.create({ data: { createdAt: date } })
|
||||||
|
.then(async () => {
|
||||||
|
latestDailyStats = await prisma.dailyStats.findFirst({
|
||||||
|
orderBy: {
|
||||||
|
createdAt: "desc"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return await prisma.dailyStats.update({
|
||||||
|
where: { createdAt: latestDailyStats.createdAt },
|
||||||
|
data: {
|
||||||
|
linksDeleted: { increment: link ? 1 : 0 },
|
||||||
|
commandResponses: { increment: command ? 1 : 0 },
|
||||||
|
timesTriggered: { increment: trigger ? 1 : 0 }
|
||||||
}
|
}
|
||||||
})
|
|
||||||
.then(async (todayStat) => {
|
|
||||||
return await prisma.dailyStats.update({
|
|
||||||
where: { createdAt: todayStat?.createdAt },
|
|
||||||
data: {
|
|
||||||
linksDeleted: { increment: link ? 1 : 0 },
|
|
||||||
commandResponses: { increment: command ? 1 : 0 },
|
|
||||||
timesTriggered: { increment: trigger ? 1 : 0 }
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user