Compare commits

...

2 Commits

Author SHA1 Message Date
werewolfkid 57fdf8e18e Merge pull request 'upgrade yarn' (#3) from cron-logging into main
Main / build-and-push-docker-image (20.x) (push) Successful in 5m21s
Reviewed-on: #3
2026-05-03 10:18:11 -04:00
werewolfkid 0cef1b671e Added documentation.
Main / build-and-push-docker-image (20.x) (pull_request) Successful in 5m27s
2026-05-03 10:12:19 -04:00
+59 -16
View File
@@ -39,6 +39,9 @@ export const resolvers = {
const { startDate, endDate } = data;
if (!startDate || !endDate) {
console.info(
"GET STATS RANGE: REFUSED - one or both dates not provided."
);
return null;
}
@@ -79,33 +82,44 @@ export const resolvers = {
) => {
const { mutationKey } = data;
// If env is not development check for the mutation key.
if (env !== "development") {
// If mutation key was not provided.
if (!mutationKey) {
console.info("INIT: REFUSED - no mutation key was provided.");
return null;
}
// If provided mutation key is invalid.
if (mutationKey !== envMutationKey) {
console.info("INIT: REFUSED - mutation key provided was incorrect.");
return null;
}
}
const date = new Date().toISOString();
let count = 0;
const createdAtDate = new Date().toISOString();
// How many tables were created
let newTablesCount = 0;
// Check for daily stats document count.
if ((await prisma.dailyStats.count()) === 0) {
await prisma.dailyStats.create({ data: { createdAt: date } });
count++;
// Make a daily stats document.
console.info("INIT: created new document");
await prisma.dailyStats.create({ data: { createdAt: createdAtDate } });
newTablesCount++;
}
// Check for a total stats document.
if ((await prisma.totalStats.count()) === 0) {
await prisma.totalStats.create({ data: { createdAt: date } });
count++;
// Make the total stats document.
console.info("INIT: created new document");
await prisma.totalStats.create({ data: { createdAt: createdAtDate } });
newTablesCount++;
}
console.info(`INIT: ${count} tables have been initialized with data.`);
return `${count} tables have been initialized with data.`;
console.info(
`INIT: ${newTablesCount} tables have been initialized with data.`
);
return `${newTablesCount} tables have been initialized with data.`;
},
cronJob: async (
_parent: unknown,
@@ -114,12 +128,15 @@ export const resolvers = {
) => {
const { mutationKey } = data;
// If env is not development check for the mutation key.
if (env !== "development") {
// If mutation key was not provided.
if (!mutationKey) {
console.info("CRONJOB: REFUSED - no mutation key was provided.");
return null;
}
// If provided mutation key is invalid.
if (mutationKey !== envMutationKey) {
console.info(
"CRONJOB: REFUSED - mutation key provided was incorrect."
@@ -130,19 +147,23 @@ export const resolvers = {
const latestDailyStats = await getLatestDailyStat();
// If there is a daily stats documents.
if (latestDailyStats !== null) {
// Check if the latest document is for today.
if (isDailyStatToday(String(latestDailyStats.createdAt))) {
console.info("Cron job refused: latest document is current date.");
return null;
}
}
const date = new Date().toISOString();
await prisma.dailyStats.create({ data: { createdAt: date } });
// Create new daily stats document.
const createdAtDate = new Date().toISOString();
await prisma.dailyStats.create({ data: { createdAt: createdAtDate } });
// Get every daily stats documents.
const allStats = await prisma.dailyStats.findMany({});
// Add all stats into one object.
const calculatedStats = allStats.reduce(
(acc, curr) => {
const links = (acc.linksDeleted += curr.linksDeleted);
@@ -162,12 +183,14 @@ export const resolvers = {
}
);
// Take the total stats document.
const totalStats = await prisma.totalStats.findFirst({
orderBy: {
createdAt: "desc"
}
});
// Update the total stats document with the new total.
console.info("CRONJOB: Creating new document.");
return await prisma.totalStats.update({
where: {
@@ -191,13 +214,15 @@ export const resolvers = {
const { groupID, groupName, groupUsername, mutationKey } = data;
const groupIDInt = BigInt(groupID);
// If env is not development check for the mutation key.
if (env !== "development") {
// If mutation key was not provided.
if (!mutationKey) {
console.info("ADD GROUP: REFUSED - no mutation key was provided.");
return null;
}
// If provided mutation key is invalid.
if (mutationKey !== envMutationKey) {
console.info(
"ADD GROUP: REFUSED - mutation key provided was incorrect."
@@ -206,15 +231,20 @@ export const resolvers = {
}
}
// Attempt to find the group's document.
const existingGroup = await prisma.groups.findUnique({
where: { telegramID: groupIDInt }
});
// If the document was found.
if (existingGroup !== null) {
// Check if the document details are incorrect.
if (
existingGroup.name !== groupName ||
existingGroup.username !== groupUsername
) {
// Return the document after updating the details.
console.info("ADD GROUP: Updated group document.");
return await prisma.groups.update({
where: {
telegramID: existingGroup.telegramID
@@ -223,10 +253,12 @@ export const resolvers = {
});
}
console.info("ADD GROUP: Created new document.");
// Return document without making changed.
return existingGroup;
}
// Return document after creating one for the group.
console.info("ADD GROUP: Created new group document.");
return await prisma.groups.create({
data: {
telegramID: groupIDInt,
@@ -241,7 +273,9 @@ export const resolvers = {
) => {
const { groupID, linksDeleted, mutationKey } = data;
// If env is not development check for the mutation key.
if (env !== "development") {
// If mutation key was not provided.
if (!mutationKey) {
console.info(
"INCREMENT GROUP: REFUSED - no mutation key was provided."
@@ -249,6 +283,7 @@ export const resolvers = {
return null;
}
// If provided mutation key is invalid.
if (mutationKey !== envMutationKey) {
console.info(
"INCREMENT GROUP: REFUSED - mutation key provided was incorrect."
@@ -257,6 +292,7 @@ export const resolvers = {
}
}
// Return updated group document after incrementing it.
console.info("INCREMENT GROUP: Incremented group document.");
return await prisma.groups.update({
where: { telegramID: BigInt(groupID) },
@@ -275,12 +311,15 @@ export const resolvers = {
) => {
const { link, command, trigger, mutationKey } = data;
// If env is not development check for the mutation key.
if (env !== "development") {
// If mutation key was not provided.
if (!mutationKey) {
console.info("INCREMENT: REFUSED - no mutation key was provided.");
return null;
}
// If provided mutation key is invalid.
if (mutationKey !== envMutationKey) {
console.info(
"INCREMENT: REFUSED - mutation key provided was incorrect."
@@ -289,12 +328,16 @@ export const resolvers = {
}
}
// Get latest daily stats.
let latestDailyStats = await getLatestDailyStat();
// If there is a daily stats documents.
if (latestDailyStats !== null) {
// Check if the latest document is for today.
if (!isDailyStatToday(String(latestDailyStats.createdAt))) {
// Create new daily stats document.
console.info("INCREMENT: Created new document.");
const date = new Date().toISOString();
await prisma.dailyStats
.create({ data: { createdAt: date } })
.then(async () => {
@@ -306,8 +349,8 @@ export const resolvers = {
});
}
// Return latest daily stats after incrementing it.
console.info("INCREMENT: Incremented information on today's document.");
return await prisma.dailyStats.update({
where: { createdAt: latestDailyStats.createdAt },
data: {