This commit is contained in:
+59
-16
@@ -39,6 +39,9 @@ export const resolvers = {
|
|||||||
const { startDate, endDate } = data;
|
const { startDate, endDate } = data;
|
||||||
|
|
||||||
if (!startDate || !endDate) {
|
if (!startDate || !endDate) {
|
||||||
|
console.info(
|
||||||
|
"GET STATS RANGE: REFUSED - one or both dates not provided."
|
||||||
|
);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,33 +82,44 @@ export const resolvers = {
|
|||||||
) => {
|
) => {
|
||||||
const { mutationKey } = data;
|
const { mutationKey } = data;
|
||||||
|
|
||||||
|
// If env is not development check for the mutation key.
|
||||||
if (env !== "development") {
|
if (env !== "development") {
|
||||||
|
// If mutation key was not provided.
|
||||||
if (!mutationKey) {
|
if (!mutationKey) {
|
||||||
console.info("INIT: REFUSED - no mutation key was provided.");
|
console.info("INIT: REFUSED - no mutation key was provided.");
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If provided mutation key is invalid.
|
||||||
if (mutationKey !== envMutationKey) {
|
if (mutationKey !== envMutationKey) {
|
||||||
console.info("INIT: REFUSED - mutation key provided was incorrect.");
|
console.info("INIT: REFUSED - mutation key provided was incorrect.");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const date = new Date().toISOString();
|
const createdAtDate = new Date().toISOString();
|
||||||
let count = 0;
|
// How many tables were created
|
||||||
|
let newTablesCount = 0;
|
||||||
|
|
||||||
|
// Check for daily stats document count.
|
||||||
if ((await prisma.dailyStats.count()) === 0) {
|
if ((await prisma.dailyStats.count()) === 0) {
|
||||||
await prisma.dailyStats.create({ data: { createdAt: date } });
|
// Make a daily stats document.
|
||||||
count++;
|
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) {
|
if ((await prisma.totalStats.count()) === 0) {
|
||||||
await prisma.totalStats.create({ data: { createdAt: date } });
|
// Make the total stats document.
|
||||||
count++;
|
console.info("INIT: created new document");
|
||||||
|
await prisma.totalStats.create({ data: { createdAt: createdAtDate } });
|
||||||
|
newTablesCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.info(`INIT: ${count} tables have been initialized with data.`);
|
console.info(
|
||||||
return `${count} tables have been initialized with data.`;
|
`INIT: ${newTablesCount} tables have been initialized with data.`
|
||||||
|
);
|
||||||
|
return `${newTablesCount} tables have been initialized with data.`;
|
||||||
},
|
},
|
||||||
cronJob: async (
|
cronJob: async (
|
||||||
_parent: unknown,
|
_parent: unknown,
|
||||||
@@ -114,12 +128,15 @@ export const resolvers = {
|
|||||||
) => {
|
) => {
|
||||||
const { mutationKey } = data;
|
const { mutationKey } = data;
|
||||||
|
|
||||||
|
// If env is not development check for the mutation key.
|
||||||
if (env !== "development") {
|
if (env !== "development") {
|
||||||
|
// If mutation key was not provided.
|
||||||
if (!mutationKey) {
|
if (!mutationKey) {
|
||||||
console.info("CRONJOB: REFUSED - no mutation key was provided.");
|
console.info("CRONJOB: REFUSED - no mutation key was provided.");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If provided mutation key is invalid.
|
||||||
if (mutationKey !== envMutationKey) {
|
if (mutationKey !== envMutationKey) {
|
||||||
console.info(
|
console.info(
|
||||||
"CRONJOB: REFUSED - mutation key provided was incorrect."
|
"CRONJOB: REFUSED - mutation key provided was incorrect."
|
||||||
@@ -130,19 +147,23 @@ export const resolvers = {
|
|||||||
|
|
||||||
const latestDailyStats = await getLatestDailyStat();
|
const latestDailyStats = await getLatestDailyStat();
|
||||||
|
|
||||||
|
// If there is a daily stats documents.
|
||||||
if (latestDailyStats !== null) {
|
if (latestDailyStats !== null) {
|
||||||
|
// Check if the latest document is for today.
|
||||||
if (isDailyStatToday(String(latestDailyStats.createdAt))) {
|
if (isDailyStatToday(String(latestDailyStats.createdAt))) {
|
||||||
console.info("Cron job refused: latest document is current date.");
|
console.info("Cron job refused: latest document is current date.");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const date = new Date().toISOString();
|
// Create new daily stats document.
|
||||||
|
const createdAtDate = new Date().toISOString();
|
||||||
await prisma.dailyStats.create({ data: { createdAt: date } });
|
await prisma.dailyStats.create({ data: { createdAt: createdAtDate } });
|
||||||
|
|
||||||
|
// Get every daily stats documents.
|
||||||
const allStats = await prisma.dailyStats.findMany({});
|
const allStats = await prisma.dailyStats.findMany({});
|
||||||
|
|
||||||
|
// Add all stats into one object.
|
||||||
const calculatedStats = allStats.reduce(
|
const calculatedStats = allStats.reduce(
|
||||||
(acc, curr) => {
|
(acc, curr) => {
|
||||||
const links = (acc.linksDeleted += curr.linksDeleted);
|
const links = (acc.linksDeleted += curr.linksDeleted);
|
||||||
@@ -162,12 +183,14 @@ export const resolvers = {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Take the total stats document.
|
||||||
const totalStats = await prisma.totalStats.findFirst({
|
const totalStats = await prisma.totalStats.findFirst({
|
||||||
orderBy: {
|
orderBy: {
|
||||||
createdAt: "desc"
|
createdAt: "desc"
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Update the total stats document with the new total.
|
||||||
console.info("CRONJOB: Creating new document.");
|
console.info("CRONJOB: Creating new document.");
|
||||||
return await prisma.totalStats.update({
|
return await prisma.totalStats.update({
|
||||||
where: {
|
where: {
|
||||||
@@ -191,13 +214,15 @@ export const resolvers = {
|
|||||||
const { groupID, groupName, groupUsername, mutationKey } = data;
|
const { groupID, groupName, groupUsername, mutationKey } = data;
|
||||||
const groupIDInt = BigInt(groupID);
|
const groupIDInt = BigInt(groupID);
|
||||||
|
|
||||||
|
// If env is not development check for the mutation key.
|
||||||
if (env !== "development") {
|
if (env !== "development") {
|
||||||
|
// If mutation key was not provided.
|
||||||
if (!mutationKey) {
|
if (!mutationKey) {
|
||||||
console.info("ADD GROUP: REFUSED - no mutation key was provided.");
|
console.info("ADD GROUP: REFUSED - no mutation key was provided.");
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If provided mutation key is invalid.
|
||||||
if (mutationKey !== envMutationKey) {
|
if (mutationKey !== envMutationKey) {
|
||||||
console.info(
|
console.info(
|
||||||
"ADD GROUP: REFUSED - mutation key provided was incorrect."
|
"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({
|
const existingGroup = await prisma.groups.findUnique({
|
||||||
where: { telegramID: groupIDInt }
|
where: { telegramID: groupIDInt }
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// If the document was found.
|
||||||
if (existingGroup !== null) {
|
if (existingGroup !== null) {
|
||||||
|
// Check if the document details are incorrect.
|
||||||
if (
|
if (
|
||||||
existingGroup.name !== groupName ||
|
existingGroup.name !== groupName ||
|
||||||
existingGroup.username !== groupUsername
|
existingGroup.username !== groupUsername
|
||||||
) {
|
) {
|
||||||
|
// Return the document after updating the details.
|
||||||
|
console.info("ADD GROUP: Updated group document.");
|
||||||
return await prisma.groups.update({
|
return await prisma.groups.update({
|
||||||
where: {
|
where: {
|
||||||
telegramID: existingGroup.telegramID
|
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 existingGroup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return document after creating one for the group.
|
||||||
|
console.info("ADD GROUP: Created new group document.");
|
||||||
return await prisma.groups.create({
|
return await prisma.groups.create({
|
||||||
data: {
|
data: {
|
||||||
telegramID: groupIDInt,
|
telegramID: groupIDInt,
|
||||||
@@ -241,7 +273,9 @@ export const resolvers = {
|
|||||||
) => {
|
) => {
|
||||||
const { groupID, linksDeleted, mutationKey } = data;
|
const { groupID, linksDeleted, mutationKey } = data;
|
||||||
|
|
||||||
|
// If env is not development check for the mutation key.
|
||||||
if (env !== "development") {
|
if (env !== "development") {
|
||||||
|
// If mutation key was not provided.
|
||||||
if (!mutationKey) {
|
if (!mutationKey) {
|
||||||
console.info(
|
console.info(
|
||||||
"INCREMENT GROUP: REFUSED - no mutation key was provided."
|
"INCREMENT GROUP: REFUSED - no mutation key was provided."
|
||||||
@@ -249,6 +283,7 @@ export const resolvers = {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If provided mutation key is invalid.
|
||||||
if (mutationKey !== envMutationKey) {
|
if (mutationKey !== envMutationKey) {
|
||||||
console.info(
|
console.info(
|
||||||
"INCREMENT GROUP: REFUSED - mutation key provided was incorrect."
|
"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.");
|
console.info("INCREMENT GROUP: Incremented group document.");
|
||||||
return await prisma.groups.update({
|
return await prisma.groups.update({
|
||||||
where: { telegramID: BigInt(groupID) },
|
where: { telegramID: BigInt(groupID) },
|
||||||
@@ -275,12 +311,15 @@ export const resolvers = {
|
|||||||
) => {
|
) => {
|
||||||
const { link, command, trigger, mutationKey } = data;
|
const { link, command, trigger, mutationKey } = data;
|
||||||
|
|
||||||
|
// If env is not development check for the mutation key.
|
||||||
if (env !== "development") {
|
if (env !== "development") {
|
||||||
|
// If mutation key was not provided.
|
||||||
if (!mutationKey) {
|
if (!mutationKey) {
|
||||||
console.info("INCREMENT: REFUSED - no mutation key was provided.");
|
console.info("INCREMENT: REFUSED - no mutation key was provided.");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If provided mutation key is invalid.
|
||||||
if (mutationKey !== envMutationKey) {
|
if (mutationKey !== envMutationKey) {
|
||||||
console.info(
|
console.info(
|
||||||
"INCREMENT: REFUSED - mutation key provided was incorrect."
|
"INCREMENT: REFUSED - mutation key provided was incorrect."
|
||||||
@@ -289,12 +328,16 @@ export const resolvers = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get latest daily stats.
|
||||||
let latestDailyStats = await getLatestDailyStat();
|
let latestDailyStats = await getLatestDailyStat();
|
||||||
|
|
||||||
|
// If there is a daily stats documents.
|
||||||
if (latestDailyStats !== null) {
|
if (latestDailyStats !== null) {
|
||||||
|
// Check if the latest document is for today.
|
||||||
if (!isDailyStatToday(String(latestDailyStats.createdAt))) {
|
if (!isDailyStatToday(String(latestDailyStats.createdAt))) {
|
||||||
|
// Create new daily stats document.
|
||||||
|
console.info("INCREMENT: Created new document.");
|
||||||
const date = new Date().toISOString();
|
const date = new Date().toISOString();
|
||||||
|
|
||||||
await prisma.dailyStats
|
await prisma.dailyStats
|
||||||
.create({ data: { createdAt: date } })
|
.create({ data: { createdAt: date } })
|
||||||
.then(async () => {
|
.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.");
|
console.info("INCREMENT: Incremented information on today's document.");
|
||||||
|
|
||||||
return await prisma.dailyStats.update({
|
return await prisma.dailyStats.update({
|
||||||
where: { createdAt: latestDailyStats.createdAt },
|
where: { createdAt: latestDailyStats.createdAt },
|
||||||
data: {
|
data: {
|
||||||
|
|||||||
Reference in New Issue
Block a user