diff --git a/controllers/challenges.ts b/controllers/challenges.ts index 8dd4721..5dab094 100644 --- a/controllers/challenges.ts +++ b/controllers/challenges.ts @@ -1,90 +1,88 @@ -import DB from "@/database/config"; -import { challenges } from "@/database/schema/challenges"; -import { eq } from "drizzle-orm"; - -export default abstract class ChallengesController { - - public static async getAllChallengesWithDetails() { - const allChallenges = await DB.instance - .select({ - id: challenges.id, - name: challenges.name, - score: challenges.score, - clubId: challenges.clubId, - }) - .from(challenges) - - return allChallenges; - } - - public static async getChallenge(id: number) { - const challenge = await DB.instance - .select({ - id: challenges.id, - name: challenges.name, - score: challenges.score, - }) - .from(challenges) - .where(eq(challenges.id, id)) - .limit(1); - - return challenge.length ? challenge[0] : null; -} - - public static async getChallengeWithDetails(id: number) { - const challenge = await DB.instance - .select({ - id: challenges.id, - name: challenges.name, - score: challenges.score, - clubId: challenges.clubId, - }) - .from(challenges) - .where(eq(challenges.id, id)); - - return challenge.length ? challenge[0] : null; - } - - - public static async createChallenge(clubId: number, score: number, name: string) { - const challenge = await DB.instance - .insert(challenges) - .values({ - clubId, - score, - name, - }) - .returning({ - id: challenges.id, - name: challenges.name, - score: challenges.score, - clubId: challenges.clubId, - }); - - return challenge.length ? challenge[0] : null; - } - - public static async updateChallenge(id: number, score?: number, name?: string) { - const challenge = await DB.instance - .update(challenges) - .set({ - score, - name, - }) - .where(eq(challenges.id, id)) - .returning({ - id: challenges.id, - name: challenges.name, - score: challenges.score, - clubId: challenges.clubId, - }); - - return challenge.length ? challenge[0] : null; - } - - public static async deleteChallenge(id: number) { - await DB.instance.delete(challenges).where(eq(challenges.id, id)); - } - - public static async getDailyChallenges() {} -} \ No newline at end of file +import DB from "@/database/config"; +import { challenges } from "@/database/schema/challenges"; +import { eq } from "drizzle-orm"; + +export default abstract class ChallengesController { + public static async getAllChallengesWithDetails() { + const allChallenges = await DB.instance + .select({ + id: challenges.id, + name: challenges.name, + score: challenges.score, + clubId: challenges.clubId + }) + .from(challenges); + + return allChallenges; + } + + public static async getChallenge(id: number) { + const challenge = await DB.instance + .select({ + id: challenges.id, + name: challenges.name, + score: challenges.score + }) + .from(challenges) + .where(eq(challenges.id, id)) + .limit(1); + + return challenge.length ? challenge[0] : null; + } + + public static async getChallengeWithDetails(id: number) { + const challenge = await DB.instance + .select({ + id: challenges.id, + name: challenges.name, + score: challenges.score, + clubId: challenges.clubId + }) + .from(challenges) + .where(eq(challenges.id, id)); + + return challenge.length ? challenge[0] : null; + } + + public static async createChallenge(clubId: number, score: number, name: string) { + const challenge = await DB.instance + .insert(challenges) + .values({ + clubId, + score, + name + }) + .returning({ + id: challenges.id, + name: challenges.name, + score: challenges.score, + clubId: challenges.clubId + }); + + return challenge.length ? challenge[0] : null; + } + + public static async updateChallenge(id: number, score?: number, name?: string) { + const challenge = await DB.instance + .update(challenges) + .set({ + score, + name + }) + .where(eq(challenges.id, id)) + .returning({ + id: challenges.id, + name: challenges.name, + score: challenges.score, + clubId: challenges.clubId + }); + + return challenge.length ? challenge[0] : null; + } + + public static async deleteChallenge(id: number) { + await DB.instance.delete(challenges).where(eq(challenges.id, id)); + } + + public static async getDailyChallenges() {} +} diff --git a/database/migrations/meta/0001_snapshot.json b/database/migrations/meta/0001_snapshot.json index b208a5e..056a489 100644 --- a/database/migrations/meta/0001_snapshot.json +++ b/database/migrations/meta/0001_snapshot.json @@ -1,422 +1,399 @@ { - "id": "1fde5eae-e2dd-41d5-bcaf-c578384bfdef", - "prevId": "df1b764a-ce7e-4b2e-9cae-4f14f9a4c96e", - "version": "7", - "dialect": "postgresql", - "tables": { - "public.acquired": { - "name": "acquired", - "schema": "", - "columns": { - "user_uuid": { - "name": "user_uuid", - "type": "uuid", - "primaryKey": false, - "notNull": true + "id": "1fde5eae-e2dd-41d5-bcaf-c578384bfdef", + "prevId": "df1b764a-ce7e-4b2e-9cae-4f14f9a4c96e", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.acquired": { + "name": "acquired", + "schema": "", + "columns": { + "user_uuid": { + "name": "user_uuid", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "challenge_id": { + "name": "challenge_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "created_by": { + "name": "created_by", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "user_uuid_idx": { + "name": "user_uuid_idx", + "columns": [ + { + "expression": "user_uuid", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "challenge_id_idx": { + "name": "challenge_id_idx", + "columns": [ + { + "expression": "challenge_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "acquired_user_uuid_users_uuid_fk": { + "name": "acquired_user_uuid_users_uuid_fk", + "tableFrom": "acquired", + "tableTo": "users", + "columnsFrom": ["user_uuid"], + "columnsTo": ["uuid"], + "onDelete": "no action", + "onUpdate": "no action" + }, + "acquired_challenge_id_challenges_id_fk": { + "name": "acquired_challenge_id_challenges_id_fk", + "tableFrom": "acquired", + "tableTo": "challenges", + "columnsFrom": ["challenge_id"], + "columnsTo": ["id"], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "acquired_user_uuid_challenge_id_pk": { + "name": "acquired_user_uuid_challenge_id_pk", + "columns": ["user_uuid", "challenge_id"] + } + }, + "uniqueConstraints": {}, + "checkConstraints": {} }, - "challenge_id": { - "name": "challenge_id", - "type": "integer", - "primaryKey": false, - "notNull": true + "public.challenges": { + "name": "challenges", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "club_id": { + "name": "club_id", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "score": { + "name": "score", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "challenges_club_id_clubs_id_fk": { + "name": "challenges_club_id_clubs_id_fk", + "tableFrom": "challenges", + "tableTo": "clubs", + "columnsFrom": ["club_id"], + "columnsTo": ["id"], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} }, - "created_by": { - "name": "created_by", - "type": "integer", - "primaryKey": false, - "notNull": false + "public.clubs": { + "name": "clubs", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "avatar_url": { + "name": "avatar_url", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "daily_date": { + "name": "daily_date", + "type": "date", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "name_unique_idx": { + "name": "name_unique_idx", + "columns": [ + { + "expression": "name", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" + "public.granters": { + "name": "granters", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "club_id": { + "name": "club_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "password": { + "name": "password", + "type": "text", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": { + "granters_club_id_clubs_id_fk": { + "name": "granters_club_id_clubs_id_fk", + "tableFrom": "granters", + "tableTo": "clubs", + "columnsFrom": ["club_id"], + "columnsTo": ["id"], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" + "public.users": { + "name": "users", + "schema": "", + "columns": { + "uuid": { + "name": "uuid", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "club_id": { + "name": "club_id", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "hashpass": { + "name": "hashpass", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "username": { + "name": "username", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "avatar_url": { + "name": "avatar_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "quote": { + "name": "quote", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "email_unique_idx": { + "name": "email_unique_idx", + "columns": [ + { + "expression": "email", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "username_unique_idx": { + "name": "username_unique_idx", + "columns": [ + { + "expression": "username", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "users_club_id_clubs_id_fk": { + "name": "users_club_id_clubs_id_fk", + "tableFrom": "users", + "tableTo": "clubs", + "columnsFrom": ["club_id"], + "columnsTo": ["id"], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} } - }, - "indexes": { - "user_uuid_idx": { - "name": "user_uuid_idx", - "columns": [ - { - "expression": "user_uuid", - "isExpression": false, - "asc": true, - "nulls": "last" - } - ], - "isUnique": false, - "concurrently": false, - "method": "btree", - "with": {} - }, - "challenge_id_idx": { - "name": "challenge_id_idx", - "columns": [ - { - "expression": "challenge_id", - "isExpression": false, - "asc": true, - "nulls": "last" - } - ], - "isUnique": false, - "concurrently": false, - "method": "btree", - "with": {} - } - }, - "foreignKeys": { - "acquired_user_uuid_users_uuid_fk": { - "name": "acquired_user_uuid_users_uuid_fk", - "tableFrom": "acquired", - "tableTo": "users", - "columnsFrom": [ - "user_uuid" - ], - "columnsTo": [ - "uuid" - ], - "onDelete": "no action", - "onUpdate": "no action" - }, - "acquired_challenge_id_challenges_id_fk": { - "name": "acquired_challenge_id_challenges_id_fk", - "tableFrom": "acquired", - "tableTo": "challenges", - "columnsFrom": [ - "challenge_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "no action", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": { - "acquired_user_uuid_challenge_id_pk": { - "name": "acquired_user_uuid_challenge_id_pk", - "columns": [ - "user_uuid", - "challenge_id" - ] - } - }, - "uniqueConstraints": {}, - "checkConstraints": {} - }, - "public.challenges": { - "name": "challenges", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "club_id": { - "name": "club_id", - "type": "integer", - "primaryKey": false, - "notNull": false - }, - "score": { - "name": "score", - "type": "integer", - "primaryKey": false, - "notNull": true - }, - "name": { - "name": "name", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - } - }, - "indexes": {}, - "foreignKeys": { - "challenges_club_id_clubs_id_fk": { - "name": "challenges_club_id_clubs_id_fk", - "tableFrom": "challenges", - "tableTo": "clubs", - "columnsFrom": [ - "club_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "no action", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": {}, - "checkConstraints": {} - }, - "public.clubs": { - "name": "clubs", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "avatar_url": { - "name": "avatar_url", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "name": { - "name": "name", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "description": { - "name": "description", - "type": "text", - "primaryKey": false, - "notNull": false - }, - "daily_date": { - "name": "daily_date", - "type": "date", - "primaryKey": false, - "notNull": false - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - } - }, - "indexes": { - "name_unique_idx": { - "name": "name_unique_idx", - "columns": [ - { - "expression": "name", - "isExpression": false, - "asc": true, - "nulls": "last" - } - ], - "isUnique": true, - "concurrently": false, - "method": "btree", - "with": {} - } - }, - "foreignKeys": {}, - "compositePrimaryKeys": {}, - "uniqueConstraints": {}, - "checkConstraints": {} }, - "public.granters": { - "name": "granters", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "club_id": { - "name": "club_id", - "type": "integer", - "primaryKey": false, - "notNull": true - }, - "email": { - "name": "email", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "password": { - "name": "password", - "type": "text", - "primaryKey": false, - "notNull": true - } - }, - "indexes": {}, - "foreignKeys": { - "granters_club_id_clubs_id_fk": { - "name": "granters_club_id_clubs_id_fk", - "tableFrom": "granters", - "tableTo": "clubs", - "columnsFrom": [ - "club_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "no action", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": {}, - "checkConstraints": {} - }, - "public.users": { - "name": "users", - "schema": "", - "columns": { - "uuid": { - "name": "uuid", - "type": "uuid", - "primaryKey": true, - "notNull": true, - "default": "gen_random_uuid()" - }, - "club_id": { - "name": "club_id", - "type": "integer", - "primaryKey": false, - "notNull": false - }, - "email": { - "name": "email", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "hashpass": { - "name": "hashpass", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "username": { - "name": "username", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "avatar_url": { - "name": "avatar_url", - "type": "text", - "primaryKey": false, - "notNull": false - }, - "quote": { - "name": "quote", - "type": "text", - "primaryKey": false, - "notNull": false - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - } - }, - "indexes": { - "email_unique_idx": { - "name": "email_unique_idx", - "columns": [ - { - "expression": "email", - "isExpression": false, - "asc": true, - "nulls": "last" - } - ], - "isUnique": true, - "concurrently": false, - "method": "btree", - "with": {} - }, - "username_unique_idx": { - "name": "username_unique_idx", - "columns": [ - { - "expression": "username", - "isExpression": false, - "asc": true, - "nulls": "last" - } - ], - "isUnique": true, - "concurrently": false, - "method": "btree", - "with": {} - } - }, - "foreignKeys": { - "users_club_id_clubs_id_fk": { - "name": "users_club_id_clubs_id_fk", - "tableFrom": "users", - "tableTo": "clubs", - "columnsFrom": [ - "club_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "no action", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": {}, - "checkConstraints": {} - } - }, - "enums": {}, - "schemas": {}, - "sequences": {}, - "views": {}, - "_meta": { - "columns": {}, + "enums": {}, "schemas": {}, - "tables": {} - } -} \ No newline at end of file + "sequences": {}, + "views": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} diff --git a/database/migrations/meta/0002_snapshot.json b/database/migrations/meta/0002_snapshot.json index 764f265..956f1a1 100644 --- a/database/migrations/meta/0002_snapshot.json +++ b/database/migrations/meta/0002_snapshot.json @@ -1,422 +1,399 @@ { - "id": "6f13d6e3-4f2a-47e7-91fd-28f78fa14c76", - "prevId": "1fde5eae-e2dd-41d5-bcaf-c578384bfdef", - "version": "7", - "dialect": "postgresql", - "tables": { - "public.acquired": { - "name": "acquired", - "schema": "", - "columns": { - "user_uuid": { - "name": "user_uuid", - "type": "uuid", - "primaryKey": false, - "notNull": true + "id": "6f13d6e3-4f2a-47e7-91fd-28f78fa14c76", + "prevId": "1fde5eae-e2dd-41d5-bcaf-c578384bfdef", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.acquired": { + "name": "acquired", + "schema": "", + "columns": { + "user_uuid": { + "name": "user_uuid", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "challenge_id": { + "name": "challenge_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "created_by": { + "name": "created_by", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "user_uuid_idx": { + "name": "user_uuid_idx", + "columns": [ + { + "expression": "user_uuid", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "challenge_id_idx": { + "name": "challenge_id_idx", + "columns": [ + { + "expression": "challenge_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "acquired_user_uuid_users_uuid_fk": { + "name": "acquired_user_uuid_users_uuid_fk", + "tableFrom": "acquired", + "tableTo": "users", + "columnsFrom": ["user_uuid"], + "columnsTo": ["uuid"], + "onDelete": "cascade", + "onUpdate": "cascade" + }, + "acquired_challenge_id_challenges_id_fk": { + "name": "acquired_challenge_id_challenges_id_fk", + "tableFrom": "acquired", + "tableTo": "challenges", + "columnsFrom": ["challenge_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "cascade" + } + }, + "compositePrimaryKeys": { + "acquired_user_uuid_challenge_id_pk": { + "name": "acquired_user_uuid_challenge_id_pk", + "columns": ["user_uuid", "challenge_id"] + } + }, + "uniqueConstraints": {}, + "checkConstraints": {} }, - "challenge_id": { - "name": "challenge_id", - "type": "integer", - "primaryKey": false, - "notNull": true + "public.challenges": { + "name": "challenges", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "club_id": { + "name": "club_id", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "score": { + "name": "score", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "challenges_club_id_clubs_id_fk": { + "name": "challenges_club_id_clubs_id_fk", + "tableFrom": "challenges", + "tableTo": "clubs", + "columnsFrom": ["club_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "cascade" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} }, - "created_by": { - "name": "created_by", - "type": "integer", - "primaryKey": false, - "notNull": false + "public.clubs": { + "name": "clubs", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "avatar_url": { + "name": "avatar_url", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "daily_date": { + "name": "daily_date", + "type": "date", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "name_unique_idx": { + "name": "name_unique_idx", + "columns": [ + { + "expression": "name", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" + "public.granters": { + "name": "granters", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "club_id": { + "name": "club_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "password": { + "name": "password", + "type": "text", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": { + "granters_club_id_clubs_id_fk": { + "name": "granters_club_id_clubs_id_fk", + "tableFrom": "granters", + "tableTo": "clubs", + "columnsFrom": ["club_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "cascade" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" + "public.users": { + "name": "users", + "schema": "", + "columns": { + "uuid": { + "name": "uuid", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "club_id": { + "name": "club_id", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "hashpass": { + "name": "hashpass", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "username": { + "name": "username", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "avatar_url": { + "name": "avatar_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "quote": { + "name": "quote", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "email_unique_idx": { + "name": "email_unique_idx", + "columns": [ + { + "expression": "email", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "username_unique_idx": { + "name": "username_unique_idx", + "columns": [ + { + "expression": "username", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "users_club_id_clubs_id_fk": { + "name": "users_club_id_clubs_id_fk", + "tableFrom": "users", + "tableTo": "clubs", + "columnsFrom": ["club_id"], + "columnsTo": ["id"], + "onDelete": "set null", + "onUpdate": "cascade" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} } - }, - "indexes": { - "user_uuid_idx": { - "name": "user_uuid_idx", - "columns": [ - { - "expression": "user_uuid", - "isExpression": false, - "asc": true, - "nulls": "last" - } - ], - "isUnique": false, - "concurrently": false, - "method": "btree", - "with": {} - }, - "challenge_id_idx": { - "name": "challenge_id_idx", - "columns": [ - { - "expression": "challenge_id", - "isExpression": false, - "asc": true, - "nulls": "last" - } - ], - "isUnique": false, - "concurrently": false, - "method": "btree", - "with": {} - } - }, - "foreignKeys": { - "acquired_user_uuid_users_uuid_fk": { - "name": "acquired_user_uuid_users_uuid_fk", - "tableFrom": "acquired", - "tableTo": "users", - "columnsFrom": [ - "user_uuid" - ], - "columnsTo": [ - "uuid" - ], - "onDelete": "cascade", - "onUpdate": "cascade" - }, - "acquired_challenge_id_challenges_id_fk": { - "name": "acquired_challenge_id_challenges_id_fk", - "tableFrom": "acquired", - "tableTo": "challenges", - "columnsFrom": [ - "challenge_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "cascade" - } - }, - "compositePrimaryKeys": { - "acquired_user_uuid_challenge_id_pk": { - "name": "acquired_user_uuid_challenge_id_pk", - "columns": [ - "user_uuid", - "challenge_id" - ] - } - }, - "uniqueConstraints": {}, - "checkConstraints": {} - }, - "public.challenges": { - "name": "challenges", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "club_id": { - "name": "club_id", - "type": "integer", - "primaryKey": false, - "notNull": false - }, - "score": { - "name": "score", - "type": "integer", - "primaryKey": false, - "notNull": true - }, - "name": { - "name": "name", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - } - }, - "indexes": {}, - "foreignKeys": { - "challenges_club_id_clubs_id_fk": { - "name": "challenges_club_id_clubs_id_fk", - "tableFrom": "challenges", - "tableTo": "clubs", - "columnsFrom": [ - "club_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "cascade" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": {}, - "checkConstraints": {} - }, - "public.clubs": { - "name": "clubs", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "avatar_url": { - "name": "avatar_url", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "name": { - "name": "name", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "description": { - "name": "description", - "type": "text", - "primaryKey": false, - "notNull": false - }, - "daily_date": { - "name": "daily_date", - "type": "date", - "primaryKey": false, - "notNull": false - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - } - }, - "indexes": { - "name_unique_idx": { - "name": "name_unique_idx", - "columns": [ - { - "expression": "name", - "isExpression": false, - "asc": true, - "nulls": "last" - } - ], - "isUnique": true, - "concurrently": false, - "method": "btree", - "with": {} - } - }, - "foreignKeys": {}, - "compositePrimaryKeys": {}, - "uniqueConstraints": {}, - "checkConstraints": {} }, - "public.granters": { - "name": "granters", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "club_id": { - "name": "club_id", - "type": "integer", - "primaryKey": false, - "notNull": true - }, - "email": { - "name": "email", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "password": { - "name": "password", - "type": "text", - "primaryKey": false, - "notNull": true - } - }, - "indexes": {}, - "foreignKeys": { - "granters_club_id_clubs_id_fk": { - "name": "granters_club_id_clubs_id_fk", - "tableFrom": "granters", - "tableTo": "clubs", - "columnsFrom": [ - "club_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "cascade" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": {}, - "checkConstraints": {} - }, - "public.users": { - "name": "users", - "schema": "", - "columns": { - "uuid": { - "name": "uuid", - "type": "uuid", - "primaryKey": true, - "notNull": true, - "default": "gen_random_uuid()" - }, - "club_id": { - "name": "club_id", - "type": "integer", - "primaryKey": false, - "notNull": false - }, - "email": { - "name": "email", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "hashpass": { - "name": "hashpass", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "username": { - "name": "username", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "avatar_url": { - "name": "avatar_url", - "type": "text", - "primaryKey": false, - "notNull": false - }, - "quote": { - "name": "quote", - "type": "text", - "primaryKey": false, - "notNull": false - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - } - }, - "indexes": { - "email_unique_idx": { - "name": "email_unique_idx", - "columns": [ - { - "expression": "email", - "isExpression": false, - "asc": true, - "nulls": "last" - } - ], - "isUnique": true, - "concurrently": false, - "method": "btree", - "with": {} - }, - "username_unique_idx": { - "name": "username_unique_idx", - "columns": [ - { - "expression": "username", - "isExpression": false, - "asc": true, - "nulls": "last" - } - ], - "isUnique": true, - "concurrently": false, - "method": "btree", - "with": {} - } - }, - "foreignKeys": { - "users_club_id_clubs_id_fk": { - "name": "users_club_id_clubs_id_fk", - "tableFrom": "users", - "tableTo": "clubs", - "columnsFrom": [ - "club_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "set null", - "onUpdate": "cascade" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": {}, - "checkConstraints": {} - } - }, - "enums": {}, - "schemas": {}, - "sequences": {}, - "views": {}, - "_meta": { - "columns": {}, + "enums": {}, "schemas": {}, - "tables": {} - } -} \ No newline at end of file + "sequences": {}, + "views": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} diff --git a/database/migrations/meta/_journal.json b/database/migrations/meta/_journal.json index 5094e86..73d005e 100644 --- a/database/migrations/meta/_journal.json +++ b/database/migrations/meta/_journal.json @@ -1,27 +1,27 @@ { - "version": "7", - "dialect": "postgresql", - "entries": [ - { - "idx": 0, - "version": "7", - "when": 1730298717234, - "tag": "0000_fair_nextwave", - "breakpoints": true - }, - { - "idx": 1, - "version": "7", - "when": 1730395641724, - "tag": "0001_low_layla_miller", - "breakpoints": true - }, - { - "idx": 2, - "version": "7", - "when": 1730396012322, - "tag": "0002_worried_ted_forrester", - "breakpoints": true - } - ] -} \ No newline at end of file + "version": "7", + "dialect": "postgresql", + "entries": [ + { + "idx": 0, + "version": "7", + "when": 1730298717234, + "tag": "0000_fair_nextwave", + "breakpoints": true + }, + { + "idx": 1, + "version": "7", + "when": 1730395641724, + "tag": "0001_low_layla_miller", + "breakpoints": true + }, + { + "idx": 2, + "version": "7", + "when": 1730396012322, + "tag": "0002_worried_ted_forrester", + "breakpoints": true + } + ] +} diff --git a/routes/admin/challenges/create.ts b/routes/admin/challenges/create.ts index fa52740..5c6d68c 100644 --- a/routes/admin/challenges/create.ts +++ b/routes/admin/challenges/create.ts @@ -1,39 +1,39 @@ -import ChallengesController from "@/controllers/challenges"; -import Status from "@/models/status"; -import { NextFunction, Request, Response } from "express"; -import { z } from "zod"; - -const body = z.object({ - clubId: z.number(), - score: z.number(), - name: z.string(), -}); - -export default async function Route_AdminChallenges_Create(req: Request, res: Response, next: NextFunction) { - const bodyPayload = body.safeParse(req.body); - - if (!bodyPayload.success) { - return Status.send(req, next, { - status: 400, - error: "errors.validation" - }); - } - - const challenge = await ChallengesController.createChallenge( - bodyPayload.data.clubId, - bodyPayload.data.score, - bodyPayload.data.name, - ); - - if (!challenge) { - return Status.send(req, next, { - status: 500, - error: "system.internal" - }); - } - - return Status.send(req, next, { - status: 201, - data: challenge - }); -} +import ChallengesController from "@/controllers/challenges"; +import Status from "@/models/status"; +import { NextFunction, Request, Response } from "express"; +import { z } from "zod"; + +const body = z.object({ + clubId: z.number(), + score: z.number(), + name: z.string() +}); + +export default async function Route_AdminChallenges_Create(req: Request, res: Response, next: NextFunction) { + const bodyPayload = body.safeParse(req.body); + + if (!bodyPayload.success) { + return Status.send(req, next, { + status: 400, + error: "errors.validation" + }); + } + + const challenge = await ChallengesController.createChallenge( + bodyPayload.data.clubId, + bodyPayload.data.score, + bodyPayload.data.name + ); + + if (!challenge) { + return Status.send(req, next, { + status: 500, + error: "system.internal" + }); + } + + return Status.send(req, next, { + status: 201, + data: challenge + }); +} diff --git a/routes/admin/challenges/delete.ts b/routes/admin/challenges/delete.ts index aab67aa..c7f1c28 100644 --- a/routes/admin/challenges/delete.ts +++ b/routes/admin/challenges/delete.ts @@ -1,35 +1,35 @@ -import ChallengesController from "@/controllers/challenges"; -import { znumber } from "@/env/extras"; -import Status from "@/models/status"; -import { NextFunction, Request, Response } from "express"; -import { z } from "zod"; - -const params = z.object({ - id: znumber() -}) - -export default async function Route_AdminChallenges_Delete(req: Request, res: Response, next: NextFunction) { - const paramsPayload = params.safeParse(req.params); - - if (!paramsPayload.success) { - return Status.send(req, next, { - status: 400, - error: "errors.validation" - }) - } - - const challenge = await ChallengesController.getChallenge(paramsPayload.data.id); - - if (!challenge) { - return Status.send(req, next, { - status: 404, - error: "errors.notFound" - }) - } - - await ChallengesController.deleteChallenge(paramsPayload.data.id); - - return Status.send(req, next, { - status: 204 -}); -} \ No newline at end of file +import ChallengesController from "@/controllers/challenges"; +import { znumber } from "@/env/extras"; +import Status from "@/models/status"; +import { NextFunction, Request, Response } from "express"; +import { z } from "zod"; + +const params = z.object({ + id: znumber() +}); + +export default async function Route_AdminChallenges_Delete(req: Request, res: Response, next: NextFunction) { + const paramsPayload = params.safeParse(req.params); + + if (!paramsPayload.success) { + return Status.send(req, next, { + status: 400, + error: "errors.validation" + }); + } + + const challenge = await ChallengesController.getChallenge(paramsPayload.data.id); + + if (!challenge) { + return Status.send(req, next, { + status: 404, + error: "errors.notFound" + }); + } + + await ChallengesController.deleteChallenge(paramsPayload.data.id); + + return Status.send(req, next, { + status: 204 + }); +} diff --git a/routes/admin/challenges/list.ts b/routes/admin/challenges/list.ts index 44d1214..b61d439 100644 --- a/routes/admin/challenges/list.ts +++ b/routes/admin/challenges/list.ts @@ -1,10 +1,10 @@ -import ChallengesController from "@/controllers/challenges"; -import Status from "@/models/status"; -import { NextFunction, Request, Response } from "express"; - -export default async function Route_AdminChallenges_List(req: Request, res: Response, next: NextFunction) { - return Status.send(req, next, { - status: 200, - data: await ChallengesController.getAllChallengesWithDetails() - }); -} \ No newline at end of file +import ChallengesController from "@/controllers/challenges"; +import Status from "@/models/status"; +import { NextFunction, Request, Response } from "express"; + +export default async function Route_AdminChallenges_List(req: Request, res: Response, next: NextFunction) { + return Status.send(req, next, { + status: 200, + data: await ChallengesController.getAllChallengesWithDetails() + }); +} diff --git a/routes/admin/challenges/router.ts b/routes/admin/challenges/router.ts index d366d12..8363328 100644 --- a/routes/admin/challenges/router.ts +++ b/routes/admin/challenges/router.ts @@ -1,14 +1,14 @@ -import { Router } from "express"; -import Route_AdminChallenges_Create from "./create"; -import Route_AdminChallenges_Delete from "./delete"; -import Route_AdminChallenges_List from "./list"; -import Route_AdminChallenges_Update from "./update"; - -const adminChallengesRouter = Router(); - -adminChallengesRouter.get("/", Route_AdminChallenges_List) -adminChallengesRouter.post("/", Route_AdminChallenges_Create) -adminChallengesRouter.put("/:id", Route_AdminChallenges_Update) -adminChallengesRouter.delete("/:id", Route_AdminChallenges_Delete) - -export default adminChallengesRouter; \ No newline at end of file +import { Router } from "express"; +import Route_AdminChallenges_Create from "./create"; +import Route_AdminChallenges_Delete from "./delete"; +import Route_AdminChallenges_List from "./list"; +import Route_AdminChallenges_Update from "./update"; + +const adminChallengesRouter = Router(); + +adminChallengesRouter.get("/", Route_AdminChallenges_List); +adminChallengesRouter.post("/", Route_AdminChallenges_Create); +adminChallengesRouter.put("/:id", Route_AdminChallenges_Update); +adminChallengesRouter.delete("/:id", Route_AdminChallenges_Delete); + +export default adminChallengesRouter; diff --git a/routes/admin/challenges/update.ts b/routes/admin/challenges/update.ts index ccef886..5a653fa 100644 --- a/routes/admin/challenges/update.ts +++ b/routes/admin/challenges/update.ts @@ -1,53 +1,53 @@ -import ChallengesController from "@/controllers/challenges"; -import { znumber } from "@/env/extras"; -import Status from "@/models/status"; -import { NextFunction, Request, Response } from "express"; -import { z } from "zod"; - -const params = z.object({ - id: znumber() -}); - -const body = z.object({ - score: z.number().optional(), - name: z.string().optional(), -}) - -export default async function Route_AdminChallenges_Update(req: Request, res: Response, next: NextFunction) { - const bodyPayload = body.safeParse(req.body); - const paramsPayload = params.safeParse(req.params); - - if (!paramsPayload.success || !bodyPayload.success) { - return Status.send(req, next, { - status: 400, - error: "errors.validation" - }); - } - - const challenge = await ChallengesController.getChallengeWithDetails(paramsPayload.data.id); - - if (!challenge) { - return Status.send(req, next, { - status: 404, - error: "errors.notFound" - }); - } - - const updatedChallenge = await ChallengesController.updateChallenge( - paramsPayload.data.id, - bodyPayload.data.score, - bodyPayload.data.name, - ); - - if (!updatedChallenge) { - return Status.send(req, next, { - status: 500, - error: "system.internal" - }); - } - - return Status.send(req, next, { - status: 200, - data: updatedChallenge - }); -} \ No newline at end of file +import ChallengesController from "@/controllers/challenges"; +import { znumber } from "@/env/extras"; +import Status from "@/models/status"; +import { NextFunction, Request, Response } from "express"; +import { z } from "zod"; + +const params = z.object({ + id: znumber() +}); + +const body = z.object({ + score: z.number().optional(), + name: z.string().optional() +}); + +export default async function Route_AdminChallenges_Update(req: Request, res: Response, next: NextFunction) { + const bodyPayload = body.safeParse(req.body); + const paramsPayload = params.safeParse(req.params); + + if (!paramsPayload.success || !bodyPayload.success) { + return Status.send(req, next, { + status: 400, + error: "errors.validation" + }); + } + + const challenge = await ChallengesController.getChallengeWithDetails(paramsPayload.data.id); + + if (!challenge) { + return Status.send(req, next, { + status: 404, + error: "errors.notFound" + }); + } + + const updatedChallenge = await ChallengesController.updateChallenge( + paramsPayload.data.id, + bodyPayload.data.score, + bodyPayload.data.name + ); + + if (!updatedChallenge) { + return Status.send(req, next, { + status: 500, + error: "system.internal" + }); + } + + return Status.send(req, next, { + status: 200, + data: updatedChallenge + }); +} diff --git a/routes/daily/challenges.ts b/routes/daily/challenges.ts index 159e4f4..7f563d7 100644 --- a/routes/daily/challenges.ts +++ b/routes/daily/challenges.ts @@ -1,9 +1,8 @@ - -/** - * Handles the /daily/challenges route. - * Retrieves the daily challenges information. - * Sends a response with a status of 200 and the challenges information. - */ -// export default async function Route_Daily_Challenges_Get(req: Request, res: Response, next: NextFunction) { -// const challenges = null -// } \ No newline at end of file +/** + * Handles the /daily/challenges route. + * Retrieves the daily challenges information. + * Sends a response with a status of 200 and the challenges information. + */ +// export default async function Route_Daily_Challenges_Get(req: Request, res: Response, next: NextFunction) { +// const challenges = null +// } diff --git a/routes/daily/router.ts b/routes/daily/router.ts index da70049..7894c32 100644 --- a/routes/daily/router.ts +++ b/routes/daily/router.ts @@ -1,9 +1,9 @@ -import { Router } from "express"; -import Route_Daily_Read from "./read"; - -const dailyRouter = Router(); - -dailyRouter.get("/", Route_Daily_Read); -// dailyRouter.get("/challenges", Route_Daily_Challenges_Get); - -export default dailyRouter; +import { Router } from "express"; +import Route_Daily_Read from "./read"; + +const dailyRouter = Router(); + +dailyRouter.get("/", Route_Daily_Read); +// dailyRouter.get("/challenges", Route_Daily_Challenges_Get); + +export default dailyRouter; diff --git a/tests/e2e/challenges.test.ts b/tests/e2e/challenges.test.ts index 1245aee..799913b 100644 --- a/tests/e2e/challenges.test.ts +++ b/tests/e2e/challenges.test.ts @@ -1,152 +1,158 @@ -import createApp from "@/app"; -import globals from "@/env/env"; -import { toDateString } from "@/utils/date"; -import { del, get, post, put } from "../utils"; - -const app = createApp("e2e-challenges"); - -const testGlobals = { - clubId: 0, - challengedId: 0, -}; - -describe("Test challenges", () => { - - test("should get permission denied", async () => { - const res = await get(app, "/admin/challenges", undefined, undefined, { - "X-ADMIN-KEY": "definitely wrong" - }); - - expect(res.body).toStrictEqual({ - masterStatus: 401, - sentAt: expect.any(Number), - response: [ - { - status: 401, - success: false, - error: "errors.auth.admin", - translatedError: "Invalid X-ADMIN-KEY header" - } - ] - }); - }); - - test("should create a club and challenge", async () => { - - const resClub = await post( - app, - "/admin/clubs", - { - name: "challenge club test", - avatarUrl: "https://placehold.co/400", - description: "description", - dailyDate: toDateString(new Date("2024-11-01")) - }, - { - "X-ADMIN-KEY": globals.env.ADMIN_TOKEN - } - ); - - testGlobals.clubId = resClub.body.response[0].data.id ?? "invalid"; - - const res = await post(app, "/admin/challenges", { - clubId: testGlobals.clubId, - score: 100, - name: "test challenge" - }, { - "X-ADMIN-KEY": globals.env.ADMIN_TOKEN - }); - - expect(res.body).toStrictEqual({ - masterStatus: 201, - sentAt: expect.any(Number), - response: [ - { - status: 201, - success: true, - data: { - id: expect.any(Number), - score: 100, - name: "test challenge", - clubId: testGlobals.clubId, - } - } - ] - }); - - testGlobals.challengedId = res.body.response[0].data.id ?? "invalid"; - - }); - - - test("should edit a challenge", async () => { - const res = await put(app, "/admin/challenges/:id", - { id: testGlobals.challengedId }, - { - name: "edited challenge" - }, { - "X-ADMIN-KEY": globals.env.ADMIN_TOKEN - }); - - expect(res.body).toStrictEqual({ - masterStatus: 200, - sentAt: expect.any(Number), - response: [ - { - status: 200, - success: true, - data: { - id: testGlobals.challengedId, - score: 100, - name: "edited challenge", - clubId: testGlobals.clubId, - } - } - ] - }); - }); - - test("should get all challenges", async () => { - const res = await get(app, "/admin/challenges", undefined, undefined, { - "X-ADMIN-KEY": globals.env.ADMIN_TOKEN - }); - - expect(res.body).toStrictEqual({ - masterStatus: 200, - sentAt: expect.any(Number), - response: [ - { - status: 200, - success: true, - data: expect.arrayContaining([ - { - id: expect.any(Number), - score: 100, - name: "edited challenge", - clubId: testGlobals.clubId, - } - ]) - } - ] - }); - }); - - test("should delete a challenge", async () => { - const res = await del(app, "/admin/challenges/:id", - { id: testGlobals.challengedId }, - { "X-ADMIN-KEY": globals.env.ADMIN_TOKEN} - ); - - expect(res.body).toStrictEqual({ - masterStatus: 204, - sentAt: expect.any(Number), - response: [ - { - status: 204, - success: true, - } - ] - }); - }); - -}); +import createApp from "@/app"; +import globals from "@/env/env"; +import { toDateString } from "@/utils/date"; +import { del, get, post, put } from "../utils"; + +const app = createApp("e2e-challenges"); + +const testGlobals = { + clubId: 0, + challengedId: 0 +}; + +describe("Test challenges", () => { + test("should get permission denied", async () => { + const res = await get(app, "/admin/challenges", undefined, undefined, { + "X-ADMIN-KEY": "definitely wrong" + }); + + expect(res.body).toStrictEqual({ + masterStatus: 401, + sentAt: expect.any(Number), + response: [ + { + status: 401, + success: false, + error: "errors.auth.admin", + translatedError: "Invalid X-ADMIN-KEY header" + } + ] + }); + }); + + test("should create a club and challenge", async () => { + const resClub = await post( + app, + "/admin/clubs", + { + name: "challenge club test", + avatarUrl: "https://placehold.co/400", + description: "description", + dailyDate: toDateString(new Date("2024-11-01")) + }, + { + "X-ADMIN-KEY": globals.env.ADMIN_TOKEN + } + ); + + testGlobals.clubId = resClub.body.response[0].data.id ?? "invalid"; + + const res = await post( + app, + "/admin/challenges", + { + clubId: testGlobals.clubId, + score: 100, + name: "test challenge" + }, + { + "X-ADMIN-KEY": globals.env.ADMIN_TOKEN + } + ); + + expect(res.body).toStrictEqual({ + masterStatus: 201, + sentAt: expect.any(Number), + response: [ + { + status: 201, + success: true, + data: { + id: expect.any(Number), + score: 100, + name: "test challenge", + clubId: testGlobals.clubId + } + } + ] + }); + + testGlobals.challengedId = res.body.response[0].data.id ?? "invalid"; + }); + + test("should edit a challenge", async () => { + const res = await put( + app, + "/admin/challenges/:id", + { id: testGlobals.challengedId }, + { + name: "edited challenge" + }, + { + "X-ADMIN-KEY": globals.env.ADMIN_TOKEN + } + ); + + expect(res.body).toStrictEqual({ + masterStatus: 200, + sentAt: expect.any(Number), + response: [ + { + status: 200, + success: true, + data: { + id: testGlobals.challengedId, + score: 100, + name: "edited challenge", + clubId: testGlobals.clubId + } + } + ] + }); + }); + + test("should get all challenges", async () => { + const res = await get(app, "/admin/challenges", undefined, undefined, { + "X-ADMIN-KEY": globals.env.ADMIN_TOKEN + }); + + expect(res.body).toStrictEqual({ + masterStatus: 200, + sentAt: expect.any(Number), + response: [ + { + status: 200, + success: true, + data: expect.arrayContaining([ + { + id: expect.any(Number), + score: 100, + name: "edited challenge", + clubId: testGlobals.clubId + } + ]) + } + ] + }); + }); + + test("should delete a challenge", async () => { + const res = await del( + app, + "/admin/challenges/:id", + { id: testGlobals.challengedId }, + { "X-ADMIN-KEY": globals.env.ADMIN_TOKEN } + ); + + expect(res.body).toStrictEqual({ + masterStatus: 204, + sentAt: expect.any(Number), + response: [ + { + status: 204, + success: true + } + ] + }); + }); +});