Secured mutations with an api-key. When in production mode it should return null if an API key is not provided or is incorrect.
All checks were successful
Main / build-and-push-docker-image (20.x) (push) Successful in 5m6s
All checks were successful
Main / build-and-push-docker-image (20.x) (push) Successful in 5m6s
This commit is contained in:
@@ -8,8 +8,6 @@ interface NextContext {
|
|||||||
params: Promise<Record<string, string>>;
|
params: Promise<Record<string, string>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(process.env);
|
|
||||||
|
|
||||||
const environment = process.env.NODE_ENV || "development";
|
const environment = process.env.NODE_ENV || "development";
|
||||||
|
|
||||||
const isValidApiKey = (apiKey: string): boolean => {
|
const isValidApiKey = (apiKey: string): boolean => {
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
import prisma from "@/lib/prismaClient";
|
import prisma from "@/lib/prismaClient";
|
||||||
import { BigIntResolver } from "graphql-scalars";
|
import { BigIntResolver } from "graphql-scalars";
|
||||||
|
|
||||||
|
const envMutationKey = process.env.MUTATION_KEY || "";
|
||||||
|
const env = process.env.NODE_ENV || "development";
|
||||||
|
|
||||||
// Prisma
|
// Prisma
|
||||||
export const resolvers = {
|
export const resolvers = {
|
||||||
// scalars
|
// scalars
|
||||||
@@ -39,76 +42,114 @@ export const resolvers = {
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
Mutation: {
|
Mutation: {
|
||||||
init: async () =>
|
init: async (
|
||||||
// _parent: unknown,
|
|
||||||
// data: { newWeek: boolean; newMonth: boolean }
|
|
||||||
// _ctx: unknown
|
|
||||||
{
|
|
||||||
const date = new Date().toISOString();
|
|
||||||
let count = 0;
|
|
||||||
|
|
||||||
if ((await prisma.dailyStats.count()) === 0) {
|
|
||||||
await prisma.dailyStats.create({ data: { createdAt: date } });
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
if ((await prisma.totalStats.count()) === 0) {
|
|
||||||
await prisma.totalStats.create({ data: { createdAt: date } });
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return `${count} tables have been initialized with data.`;
|
|
||||||
},
|
|
||||||
cronJob: async () =>
|
|
||||||
// _parent: unknown,
|
|
||||||
// data: { newWeek: boolean; newMonth: boolean }
|
|
||||||
// _ctx: unknown
|
|
||||||
{
|
|
||||||
const date = new Date().toISOString();
|
|
||||||
|
|
||||||
await prisma.dailyStats.create({ data: { createdAt: date } });
|
|
||||||
|
|
||||||
const allStats = await prisma.dailyStats.findMany({});
|
|
||||||
|
|
||||||
const calculatedStats = allStats.reduce(
|
|
||||||
(acc, curr) => {
|
|
||||||
const links = (acc.linksDeleted += curr.linksDeleted);
|
|
||||||
const commands = (acc.commandResponses += curr.commandResponses);
|
|
||||||
const triggers = (acc.timesTriggered += curr.timesTriggered);
|
|
||||||
|
|
||||||
return {
|
|
||||||
linksDeleted: links,
|
|
||||||
commandResponses: commands,
|
|
||||||
timesTriggered: triggers
|
|
||||||
};
|
|
||||||
},
|
|
||||||
{
|
|
||||||
linksDeleted: 0,
|
|
||||||
commandResponses: 0,
|
|
||||||
timesTriggered: 0
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
const totalStats = await prisma.totalStats.findFirst({
|
|
||||||
orderBy: {
|
|
||||||
createdAt: "desc"
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return await prisma.totalStats.update({
|
|
||||||
where: {
|
|
||||||
createdAt: totalStats?.createdAt
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
...calculatedStats
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
addGroup: async (
|
|
||||||
_parent: unknown,
|
_parent: unknown,
|
||||||
data: { groupID: number; groupName: string; groupUsername: string }
|
data: { mutationKey?: string }
|
||||||
// _ctx: unknown
|
// _ctx: unknown
|
||||||
) => {
|
) => {
|
||||||
const { groupID, groupName, groupUsername } = data;
|
const { mutationKey } = data;
|
||||||
|
|
||||||
|
if (env !== "development") {
|
||||||
|
if (!mutationKey) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mutationKey !== envMutationKey) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const date = new Date().toISOString();
|
||||||
|
let count = 0;
|
||||||
|
|
||||||
|
if ((await prisma.dailyStats.count()) === 0) {
|
||||||
|
await prisma.dailyStats.create({ data: { createdAt: date } });
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
if ((await prisma.totalStats.count()) === 0) {
|
||||||
|
await prisma.totalStats.create({ data: { createdAt: date } });
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return `${count} tables have been initialized with data.`;
|
||||||
|
},
|
||||||
|
cronJob: async (
|
||||||
|
_parent: unknown,
|
||||||
|
data: { mutationKey?: string }
|
||||||
|
// _ctx: unknown
|
||||||
|
) => {
|
||||||
|
const { mutationKey } = data;
|
||||||
|
|
||||||
|
if (env !== "development") {
|
||||||
|
if (!mutationKey) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mutationKey !== envMutationKey) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const date = new Date().toISOString();
|
||||||
|
|
||||||
|
await prisma.dailyStats.create({ data: { createdAt: date } });
|
||||||
|
|
||||||
|
const allStats = await prisma.dailyStats.findMany({});
|
||||||
|
|
||||||
|
const calculatedStats = allStats.reduce(
|
||||||
|
(acc, curr) => {
|
||||||
|
const links = (acc.linksDeleted += curr.linksDeleted);
|
||||||
|
const commands = (acc.commandResponses += curr.commandResponses);
|
||||||
|
const triggers = (acc.timesTriggered += curr.timesTriggered);
|
||||||
|
|
||||||
|
return {
|
||||||
|
linksDeleted: links,
|
||||||
|
commandResponses: commands,
|
||||||
|
timesTriggered: triggers
|
||||||
|
};
|
||||||
|
},
|
||||||
|
{
|
||||||
|
linksDeleted: 0,
|
||||||
|
commandResponses: 0,
|
||||||
|
timesTriggered: 0
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const totalStats = await prisma.totalStats.findFirst({
|
||||||
|
orderBy: {
|
||||||
|
createdAt: "desc"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return await prisma.totalStats.update({
|
||||||
|
where: {
|
||||||
|
createdAt: totalStats?.createdAt
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
...calculatedStats
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
addGroup: async (
|
||||||
|
_parent: unknown,
|
||||||
|
data: {
|
||||||
|
groupID: number;
|
||||||
|
groupName: string;
|
||||||
|
groupUsername: string;
|
||||||
|
mutationKey?: string;
|
||||||
|
}
|
||||||
|
// _ctx: unknown
|
||||||
|
) => {
|
||||||
|
const { groupID, groupName, groupUsername, mutationKey } = data;
|
||||||
|
|
||||||
|
if (env !== "development") {
|
||||||
|
if (!mutationKey) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mutationKey !== envMutationKey) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const existingGroup = await prisma.groups.findFirst({
|
const existingGroup = await prisma.groups.findFirst({
|
||||||
where: { telegramID: groupID }
|
where: { telegramID: groupID }
|
||||||
@@ -139,10 +180,20 @@ export const resolvers = {
|
|||||||
},
|
},
|
||||||
incrementGroup: async (
|
incrementGroup: async (
|
||||||
_parent: unknown,
|
_parent: unknown,
|
||||||
data: { groupID: number; linksDeleted: number }
|
data: { groupID: number; linksDeleted: number; mutationKey?: string }
|
||||||
// _ctx: unknown
|
// _ctx: unknown
|
||||||
) => {
|
) => {
|
||||||
const { groupID, linksDeleted } = data;
|
const { groupID, linksDeleted, mutationKey } = data;
|
||||||
|
|
||||||
|
if (env !== "development") {
|
||||||
|
if (!mutationKey) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mutationKey !== envMutationKey) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return await prisma.groups.update({
|
return await prisma.groups.update({
|
||||||
where: { telegramID: groupID },
|
where: { telegramID: groupID },
|
||||||
@@ -151,10 +202,25 @@ export const resolvers = {
|
|||||||
},
|
},
|
||||||
increment: async (
|
increment: async (
|
||||||
_parent: unknown,
|
_parent: unknown,
|
||||||
data: { link: boolean; command: boolean; trigger: boolean }
|
data: {
|
||||||
|
link: boolean;
|
||||||
|
command: boolean;
|
||||||
|
trigger: boolean;
|
||||||
|
mutationKey?: string;
|
||||||
|
}
|
||||||
// _ctx: unknown
|
// _ctx: unknown
|
||||||
) => {
|
) => {
|
||||||
const { link, command, trigger } = data;
|
const { link, command, trigger, mutationKey } = data;
|
||||||
|
|
||||||
|
if (env !== "development") {
|
||||||
|
if (!mutationKey) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mutationKey !== envMutationKey) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return await prisma.dailyStats
|
return await prisma.dailyStats
|
||||||
.findFirst({
|
.findFirst({
|
||||||
|
|||||||
@@ -11,11 +11,25 @@ const typeDefs = /* GraphQL */ `
|
|||||||
getTotalStats: TotalStats!
|
getTotalStats: TotalStats!
|
||||||
}
|
}
|
||||||
type Mutation {
|
type Mutation {
|
||||||
init: String!
|
init(mutationKey: String): String
|
||||||
cronJob: TotalStats!
|
cronJob(mutationKey: String): TotalStats
|
||||||
addGroup(groupID: BigInt, groupName: String, groupUsername: String): Groups!
|
addGroup(
|
||||||
incrementGroup(groupID: BigInt, linksDeleted: Int): Groups!
|
groupID: BigInt!
|
||||||
increment(link: Boolean, command: Boolean, trigger: Boolean): DailyStats!
|
groupName: String!
|
||||||
|
groupUsername: String
|
||||||
|
mutationKey: String
|
||||||
|
): Groups
|
||||||
|
incrementGroup(
|
||||||
|
groupID: BigInt!
|
||||||
|
linksDeleted: Int!
|
||||||
|
mutationKey: String
|
||||||
|
): Groups
|
||||||
|
increment(
|
||||||
|
link: Boolean
|
||||||
|
command: Boolean
|
||||||
|
trigger: Boolean
|
||||||
|
mutationKey: String
|
||||||
|
): DailyStats
|
||||||
}
|
}
|
||||||
|
|
||||||
type Groups {
|
type Groups {
|
||||||
|
|||||||
Reference in New Issue
Block a user