diff --git a/src/db/migrations/0001_eager_living_lightning.sql b/src/db/migrations/0001_eager_living_lightning.sql deleted file mode 100644 index b7a7988a4b94d9aaa845d75e81f19cefc894f51a..0000000000000000000000000000000000000000 --- a/src/db/migrations/0001_eager_living_lightning.sql +++ /dev/null @@ -1,336 +0,0 @@ -CREATE TABLE IF NOT EXISTS "collection_likes" ( - "user_id" integer NOT NULL, - "collection" integer NOT NULL -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "collection_resources" ( - "collection_id" integer NOT NULL, - "resource_id" integer NOT NULL -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "collection_stats" ( - "id" serial PRIMARY KEY NOT NULL, - "collection_id" bigint NOT NULL, - "views" bigint DEFAULT 0, - "downloads" bigint DEFAULT 0, - "likes" bigint DEFAULT 0, - "shares" bigint DEFAULT 0, - "score" bigint DEFAULT 0, - "follows" bigint DEFAULT 0, - CONSTRAINT "collection_stats_id_unique" UNIQUE("id") -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "collection" ( - "id" serial PRIMARY KEY NOT NULL, - "name" varchar(255), - "description" text, - "is_private" boolean, - "is_active" boolean DEFAULT true, - "created_at" timestamp DEFAULT now() NOT NULL, - "updated_at" timestamp, - "deleted_at" timestamp, - "thumbnail" varchar(255), - CONSTRAINT "collection_id_unique" UNIQUE("id") -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "complaint" ( - "id" serial PRIMARY KEY NOT NULL, - "state" integer DEFAULT 0 NOT NULL, - "description" text NOT NULL, - "evaluatedUser" integer NOT NULL, - "resource_id" integer, - "collection_id" integer, - "user_id" integer, - "evaluated_at" timestamp, - "created_at" timestamp DEFAULT now() NOT NULL, - "q1" boolean, - "q2" boolean, - "q3" boolean, - "q4" boolean -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "educational_stages" ( - "id" serial PRIMARY KEY NOT NULL, - "name" varchar NOT NULL, - CONSTRAINT "educational_stages_id_unique" UNIQUE("id"), - CONSTRAINT "educational_stages_name_unique" UNIQUE("name") -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "follows" ( - "id" serial PRIMARY KEY NOT NULL, - "user_id" integer NOT NULL, - "follower_id" integer NOT NULL, - CONSTRAINT "follows_id_unique" UNIQUE("id") -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "language" ( - "id" serial PRIMARY KEY NOT NULL, - "name" varchar NOT NULL, - "code" varchar NOT NULL, - CONSTRAINT "language_id_unique" UNIQUE("id"), - CONSTRAINT "language_name_unique" UNIQUE("name"), - CONSTRAINT "language_code_unique" UNIQUE("code") -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "licenses" ( - "id" serial PRIMARY KEY NOT NULL, - "name" varchar NOT NULL, - "description" varchar NOT NULL, - "url" varchar NOT NULL, - CONSTRAINT "licenses_id_unique" UNIQUE("id"), - CONSTRAINT "licenses_name_unique" UNIQUE("name") -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "object_types" ( - "id" serial PRIMARY KEY NOT NULL, - "name" varchar(255) NOT NULL, - CONSTRAINT "object_types_id_unique" UNIQUE("id") -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "reset_ticket" ( - "id" serial PRIMARY KEY NOT NULL, - "user_id" integer NOT NULL, - "token_hash" varchar(255) NOT NULL, - "expiration_date" timestamp NOT NULL, - "token_used" boolean DEFAULT false NOT NULL, - "valid_token" boolean DEFAULT true NOT NULL, - "created_at" timestamp DEFAULT now() NOT NULL, - "updated_at" timestamp DEFAULT now() NOT NULL, - CONSTRAINT "reset_ticket_id_unique" UNIQUE("id") -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "resource_languages" ( - "resource_id" integer NOT NULL, - "language_id" integer NOT NULL -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "resource_likes" ( - "user_id" integer NOT NULL, - "resource_id" integer NOT NULL -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "resource" ( - "id" serial PRIMARY KEY NOT NULL, - "name" varchar(256) NOT NULL, - "author" varchar(256) NOT NULL, - "description" varchar(256), - "bucket_key" varchar(256), - "link" varchar(256), - "thumbnail" varchar(256) NOT NULL, - "active" boolean DEFAULT false NOT NULL, - "published_at" timestamp, - "submited_at" timestamp, - "created_at" timestamp DEFAULT now() NOT NULL, - "updated_at" timestamp, - "deleted_at" timestamp, - CONSTRAINT "resource_id_unique" UNIQUE("id"), - CONSTRAINT "resource_bucket_key_unique" UNIQUE("bucket_key") -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "role" ( - "id" serial PRIMARY KEY NOT NULL, - "name" varchar(255) NOT NULL, - "description" text, - "created_at" timestamp DEFAULT now() NOT NULL, - "updated_at" timestamp DEFAULT now() NOT NULL, - CONSTRAINT "role_id_unique" UNIQUE("id"), - CONSTRAINT "role_name_unique" UNIQUE("name") -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "stats_resources" ( - "id" serial PRIMARY KEY NOT NULL, - "resource_id" bigint NOT NULL, - "views" bigint DEFAULT 0 NOT NULL, - "downloads" bigint DEFAULT 0 NOT NULL, - "shares" bigint DEFAULT 0 NOT NULL, - "score" bigint DEFAULT 0 NOT NULL, - CONSTRAINT "stats_resources_id_unique" UNIQUE("id") -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "subjects" ( - "id" serial PRIMARY KEY NOT NULL, - "name" varchar NOT NULL, - CONSTRAINT "subjects_id_unique" UNIQUE("id"), - CONSTRAINT "subjects_name_unique" UNIQUE("name") -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "submission" ( - "id" serial PRIMARY KEY NOT NULL, - "is_accepted" boolean DEFAULT false NOT NULL, - "justification" text, - "resource_id" integer NOT NULL, - "submitter_id" integer NOT NULL, - "curator_id" integer NOT NULL, - "created_at" timestamp DEFAULT now() NOT NULL, - "updated_at" timestamp, - "answered_at" timestamp, - "q1" boolean, - "q2" boolean, - "q3" boolean, - "q4" boolean -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "user_role" ( - "id" serial PRIMARY KEY NOT NULL, - "user_id" integer NOT NULL, - "role_id" integer NOT NULL, - CONSTRAINT "user_role_id_unique" UNIQUE("id") -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "user_stats" ( - "id" serial PRIMARY KEY NOT NULL, - "user_id" integer NOT NULL, - "score" integer DEFAULT 0 NOT NULL, - "likes" integer DEFAULT 0 NOT NULL, - "likes_received" integer DEFAULT 0 NOT NULL, - "follows" integer DEFAULT 0 NOT NULL, - "followers" integer DEFAULT 0 NOT NULL, - "collections" integer DEFAULT 0 NOT NULL, - "submitted_resources" integer DEFAULT 0 NOT NULL, - "approved_resources" integer DEFAULT 0 NOT NULL, - "reviewed_resources" integer DEFAULT 0 NOT NULL, - "comments" integer DEFAULT 0 NOT NULL, - CONSTRAINT "user_stats_id_unique" UNIQUE("id") -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "user" ( - "id" serial PRIMARY KEY NOT NULL, - "name" varchar(255) NOT NULL, - "username" varchar(255) NOT NULL, - "password" varchar(255) NOT NULL, - "email" varchar(255) NOT NULL, - "description" text DEFAULT 'sem descrição', - "institution" text DEFAULT 'sem instituição', - "birthday" timestamp NOT NULL, - "cpf" varchar(255) NOT NULL, - "created_at" timestamp DEFAULT now() NOT NULL, - "updated_at" timestamp DEFAULT now() NOT NULL, - "confirmed_at" timestamp, - "confirmation_sent_at" timestamp, - "deleted_at" timestamp, - "reactivated_at" timestamp, - "active" boolean DEFAULT true, - CONSTRAINT "user_id_unique" UNIQUE("id"), - CONSTRAINT "user_username_unique" UNIQUE("username"), - CONSTRAINT "user_email_unique" UNIQUE("email") -); ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "collection_likes" ADD CONSTRAINT "collection_likes_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "collection_likes" ADD CONSTRAINT "collection_likes_collection_collection_id_fk" FOREIGN KEY ("collection") REFERENCES "public"."collection"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "collection_resources" ADD CONSTRAINT "collection_resources_collection_id_collection_id_fk" FOREIGN KEY ("collection_id") REFERENCES "public"."collection"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "collection_resources" ADD CONSTRAINT "collection_resources_resource_id_resource_id_fk" FOREIGN KEY ("resource_id") REFERENCES "public"."resource"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "collection_stats" ADD CONSTRAINT "collection_stats_collection_id_collection_id_fk" FOREIGN KEY ("collection_id") REFERENCES "public"."collection"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "complaint" ADD CONSTRAINT "complaint_evaluatedUser_user_id_fk" FOREIGN KEY ("evaluatedUser") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "follows" ADD CONSTRAINT "follows_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "follows" ADD CONSTRAINT "follows_follower_id_user_id_fk" FOREIGN KEY ("follower_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "reset_ticket" ADD CONSTRAINT "reset_ticket_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "resource_languages" ADD CONSTRAINT "resource_languages_resource_id_resource_id_fk" FOREIGN KEY ("resource_id") REFERENCES "public"."resource"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "resource_languages" ADD CONSTRAINT "resource_languages_language_id_language_id_fk" FOREIGN KEY ("language_id") REFERENCES "public"."language"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "resource_likes" ADD CONSTRAINT "resource_likes_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "resource_likes" ADD CONSTRAINT "resource_likes_resource_id_resource_id_fk" FOREIGN KEY ("resource_id") REFERENCES "public"."resource"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "stats_resources" ADD CONSTRAINT "stats_resources_resource_id_resource_id_fk" FOREIGN KEY ("resource_id") REFERENCES "public"."resource"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "submission" ADD CONSTRAINT "submission_resource_id_resource_id_fk" FOREIGN KEY ("resource_id") REFERENCES "public"."resource"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "submission" ADD CONSTRAINT "submission_submitter_id_user_id_fk" FOREIGN KEY ("submitter_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "submission" ADD CONSTRAINT "submission_curator_id_user_id_fk" FOREIGN KEY ("curator_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "user_role" ADD CONSTRAINT "user_role_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "user_role" ADD CONSTRAINT "user_role_role_id_role_id_fk" FOREIGN KEY ("role_id") REFERENCES "public"."role"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "user_stats" ADD CONSTRAINT "user_stats_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; diff --git a/src/db/migrations/meta/0001_snapshot.json b/src/db/migrations/meta/0001_snapshot.json deleted file mode 100644 index 5a466332f3ddc1f5fd16a3173dac42ca55a7d193..0000000000000000000000000000000000000000 --- a/src/db/migrations/meta/0001_snapshot.json +++ /dev/null @@ -1,1499 +0,0 @@ -{ - "id": "8c1d7047-0236-4058-81d4-b0f19d54c275", - "prevId": "00000000-0000-0000-0000-000000000000", - "version": "7", - "dialect": "postgresql", - "tables": { - "public.collection_likes": { - "name": "collection_likes", - "schema": "", - "columns": { - "user_id": { - "name": "user_id", - "type": "integer", - "primaryKey": false, - "notNull": true - }, - "collection": { - "name": "collection", - "type": "integer", - "primaryKey": false, - "notNull": true - } - }, - "indexes": {}, - "foreignKeys": { - "collection_likes_user_id_user_id_fk": { - "name": "collection_likes_user_id_user_id_fk", - "tableFrom": "collection_likes", - "tableTo": "user", - "columnsFrom": [ - "user_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - }, - "collection_likes_collection_collection_id_fk": { - "name": "collection_likes_collection_collection_id_fk", - "tableFrom": "collection_likes", - "tableTo": "collection", - "columnsFrom": [ - "collection" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": {} - }, - "public.collection_resources": { - "name": "collection_resources", - "schema": "", - "columns": { - "collection_id": { - "name": "collection_id", - "type": "integer", - "primaryKey": false, - "notNull": true - }, - "resource_id": { - "name": "resource_id", - "type": "integer", - "primaryKey": false, - "notNull": true - } - }, - "indexes": {}, - "foreignKeys": { - "collection_resources_collection_id_collection_id_fk": { - "name": "collection_resources_collection_id_collection_id_fk", - "tableFrom": "collection_resources", - "tableTo": "collection", - "columnsFrom": [ - "collection_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - }, - "collection_resources_resource_id_resource_id_fk": { - "name": "collection_resources_resource_id_resource_id_fk", - "tableFrom": "collection_resources", - "tableTo": "resource", - "columnsFrom": [ - "resource_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": {} - }, - "public.collection_stats": { - "name": "collection_stats", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "collection_id": { - "name": "collection_id", - "type": "bigint", - "primaryKey": false, - "notNull": true - }, - "views": { - "name": "views", - "type": "bigint", - "primaryKey": false, - "notNull": false, - "default": 0 - }, - "downloads": { - "name": "downloads", - "type": "bigint", - "primaryKey": false, - "notNull": false, - "default": 0 - }, - "likes": { - "name": "likes", - "type": "bigint", - "primaryKey": false, - "notNull": false, - "default": 0 - }, - "shares": { - "name": "shares", - "type": "bigint", - "primaryKey": false, - "notNull": false, - "default": 0 - }, - "score": { - "name": "score", - "type": "bigint", - "primaryKey": false, - "notNull": false, - "default": 0 - }, - "follows": { - "name": "follows", - "type": "bigint", - "primaryKey": false, - "notNull": false, - "default": 0 - } - }, - "indexes": {}, - "foreignKeys": { - "collection_stats_collection_id_collection_id_fk": { - "name": "collection_stats_collection_id_collection_id_fk", - "tableFrom": "collection_stats", - "tableTo": "collection", - "columnsFrom": [ - "collection_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "collection_stats_id_unique": { - "name": "collection_stats_id_unique", - "nullsNotDistinct": false, - "columns": [ - "id" - ] - } - } - }, - "public.collection": { - "name": "collection", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "name": { - "name": "name", - "type": "varchar(255)", - "primaryKey": false, - "notNull": false - }, - "description": { - "name": "description", - "type": "text", - "primaryKey": false, - "notNull": false - }, - "is_private": { - "name": "is_private", - "type": "boolean", - "primaryKey": false, - "notNull": false - }, - "is_active": { - "name": "is_active", - "type": "boolean", - "primaryKey": false, - "notNull": false, - "default": true - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": false - }, - "deleted_at": { - "name": "deleted_at", - "type": "timestamp", - "primaryKey": false, - "notNull": false - }, - "thumbnail": { - "name": "thumbnail", - "type": "varchar(255)", - "primaryKey": false, - "notNull": false - } - }, - "indexes": {}, - "foreignKeys": {}, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "collection_id_unique": { - "name": "collection_id_unique", - "nullsNotDistinct": false, - "columns": [ - "id" - ] - } - } - }, - "public.complaint": { - "name": "complaint", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "state": { - "name": "state", - "type": "integer", - "primaryKey": false, - "notNull": true, - "default": 0 - }, - "description": { - "name": "description", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "evaluatedUser": { - "name": "evaluatedUser", - "type": "integer", - "primaryKey": false, - "notNull": true - }, - "resource_id": { - "name": "resource_id", - "type": "integer", - "primaryKey": false, - "notNull": false - }, - "collection_id": { - "name": "collection_id", - "type": "integer", - "primaryKey": false, - "notNull": false - }, - "user_id": { - "name": "user_id", - "type": "integer", - "primaryKey": false, - "notNull": false - }, - "evaluated_at": { - "name": "evaluated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": false - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "q1": { - "name": "q1", - "type": "boolean", - "primaryKey": false, - "notNull": false - }, - "q2": { - "name": "q2", - "type": "boolean", - "primaryKey": false, - "notNull": false - }, - "q3": { - "name": "q3", - "type": "boolean", - "primaryKey": false, - "notNull": false - }, - "q4": { - "name": "q4", - "type": "boolean", - "primaryKey": false, - "notNull": false - } - }, - "indexes": {}, - "foreignKeys": { - "complaint_evaluatedUser_user_id_fk": { - "name": "complaint_evaluatedUser_user_id_fk", - "tableFrom": "complaint", - "tableTo": "user", - "columnsFrom": [ - "evaluatedUser" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": {} - }, - "public.educational_stages": { - "name": "educational_stages", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "name": { - "name": "name", - "type": "varchar", - "primaryKey": false, - "notNull": true - } - }, - "indexes": {}, - "foreignKeys": {}, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "educational_stages_id_unique": { - "name": "educational_stages_id_unique", - "nullsNotDistinct": false, - "columns": [ - "id" - ] - }, - "educational_stages_name_unique": { - "name": "educational_stages_name_unique", - "nullsNotDistinct": false, - "columns": [ - "name" - ] - } - } - }, - "public.follows": { - "name": "follows", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "user_id": { - "name": "user_id", - "type": "integer", - "primaryKey": false, - "notNull": true - }, - "follower_id": { - "name": "follower_id", - "type": "integer", - "primaryKey": false, - "notNull": true - } - }, - "indexes": {}, - "foreignKeys": { - "follows_user_id_user_id_fk": { - "name": "follows_user_id_user_id_fk", - "tableFrom": "follows", - "tableTo": "user", - "columnsFrom": [ - "user_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - }, - "follows_follower_id_user_id_fk": { - "name": "follows_follower_id_user_id_fk", - "tableFrom": "follows", - "tableTo": "user", - "columnsFrom": [ - "follower_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "follows_id_unique": { - "name": "follows_id_unique", - "nullsNotDistinct": false, - "columns": [ - "id" - ] - } - } - }, - "public.language": { - "name": "language", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "name": { - "name": "name", - "type": "varchar", - "primaryKey": false, - "notNull": true - }, - "code": { - "name": "code", - "type": "varchar", - "primaryKey": false, - "notNull": true - } - }, - "indexes": {}, - "foreignKeys": {}, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "language_id_unique": { - "name": "language_id_unique", - "nullsNotDistinct": false, - "columns": [ - "id" - ] - }, - "language_name_unique": { - "name": "language_name_unique", - "nullsNotDistinct": false, - "columns": [ - "name" - ] - }, - "language_code_unique": { - "name": "language_code_unique", - "nullsNotDistinct": false, - "columns": [ - "code" - ] - } - } - }, - "public.licenses": { - "name": "licenses", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "name": { - "name": "name", - "type": "varchar", - "primaryKey": false, - "notNull": true - }, - "description": { - "name": "description", - "type": "varchar", - "primaryKey": false, - "notNull": true - }, - "url": { - "name": "url", - "type": "varchar", - "primaryKey": false, - "notNull": true - } - }, - "indexes": {}, - "foreignKeys": {}, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "licenses_id_unique": { - "name": "licenses_id_unique", - "nullsNotDistinct": false, - "columns": [ - "id" - ] - }, - "licenses_name_unique": { - "name": "licenses_name_unique", - "nullsNotDistinct": false, - "columns": [ - "name" - ] - } - } - }, - "public.object_types": { - "name": "object_types", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "name": { - "name": "name", - "type": "varchar(255)", - "primaryKey": false, - "notNull": true - } - }, - "indexes": {}, - "foreignKeys": {}, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "object_types_id_unique": { - "name": "object_types_id_unique", - "nullsNotDistinct": false, - "columns": [ - "id" - ] - } - } - }, - "public.reset_ticket": { - "name": "reset_ticket", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "user_id": { - "name": "user_id", - "type": "integer", - "primaryKey": false, - "notNull": true - }, - "token_hash": { - "name": "token_hash", - "type": "varchar(255)", - "primaryKey": false, - "notNull": true - }, - "expiration_date": { - "name": "expiration_date", - "type": "timestamp", - "primaryKey": false, - "notNull": true - }, - "token_used": { - "name": "token_used", - "type": "boolean", - "primaryKey": false, - "notNull": true, - "default": false - }, - "valid_token": { - "name": "valid_token", - "type": "boolean", - "primaryKey": false, - "notNull": true, - "default": true - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - } - }, - "indexes": {}, - "foreignKeys": { - "reset_ticket_user_id_user_id_fk": { - "name": "reset_ticket_user_id_user_id_fk", - "tableFrom": "reset_ticket", - "tableTo": "user", - "columnsFrom": [ - "user_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "reset_ticket_id_unique": { - "name": "reset_ticket_id_unique", - "nullsNotDistinct": false, - "columns": [ - "id" - ] - } - } - }, - "public.resource_languages": { - "name": "resource_languages", - "schema": "", - "columns": { - "resource_id": { - "name": "resource_id", - "type": "integer", - "primaryKey": false, - "notNull": true - }, - "language_id": { - "name": "language_id", - "type": "integer", - "primaryKey": false, - "notNull": true - } - }, - "indexes": {}, - "foreignKeys": { - "resource_languages_resource_id_resource_id_fk": { - "name": "resource_languages_resource_id_resource_id_fk", - "tableFrom": "resource_languages", - "tableTo": "resource", - "columnsFrom": [ - "resource_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - }, - "resource_languages_language_id_language_id_fk": { - "name": "resource_languages_language_id_language_id_fk", - "tableFrom": "resource_languages", - "tableTo": "language", - "columnsFrom": [ - "language_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": {} - }, - "public.resource_likes": { - "name": "resource_likes", - "schema": "", - "columns": { - "user_id": { - "name": "user_id", - "type": "integer", - "primaryKey": false, - "notNull": true - }, - "resource_id": { - "name": "resource_id", - "type": "integer", - "primaryKey": false, - "notNull": true - } - }, - "indexes": {}, - "foreignKeys": { - "resource_likes_user_id_user_id_fk": { - "name": "resource_likes_user_id_user_id_fk", - "tableFrom": "resource_likes", - "tableTo": "user", - "columnsFrom": [ - "user_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - }, - "resource_likes_resource_id_resource_id_fk": { - "name": "resource_likes_resource_id_resource_id_fk", - "tableFrom": "resource_likes", - "tableTo": "resource", - "columnsFrom": [ - "resource_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": {} - }, - "public.resource": { - "name": "resource", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "name": { - "name": "name", - "type": "varchar(256)", - "primaryKey": false, - "notNull": true - }, - "author": { - "name": "author", - "type": "varchar(256)", - "primaryKey": false, - "notNull": true - }, - "description": { - "name": "description", - "type": "varchar(256)", - "primaryKey": false, - "notNull": false - }, - "bucket_key": { - "name": "bucket_key", - "type": "varchar(256)", - "primaryKey": false, - "notNull": false - }, - "link": { - "name": "link", - "type": "varchar(256)", - "primaryKey": false, - "notNull": false - }, - "thumbnail": { - "name": "thumbnail", - "type": "varchar(256)", - "primaryKey": false, - "notNull": true - }, - "active": { - "name": "active", - "type": "boolean", - "primaryKey": false, - "notNull": true, - "default": false - }, - "published_at": { - "name": "published_at", - "type": "timestamp", - "primaryKey": false, - "notNull": false - }, - "submited_at": { - "name": "submited_at", - "type": "timestamp", - "primaryKey": false, - "notNull": false - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": false - }, - "deleted_at": { - "name": "deleted_at", - "type": "timestamp", - "primaryKey": false, - "notNull": false - } - }, - "indexes": {}, - "foreignKeys": {}, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "resource_id_unique": { - "name": "resource_id_unique", - "nullsNotDistinct": false, - "columns": [ - "id" - ] - }, - "resource_bucket_key_unique": { - "name": "resource_bucket_key_unique", - "nullsNotDistinct": false, - "columns": [ - "bucket_key" - ] - } - } - }, - "public.role": { - "name": "role", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "name": { - "name": "name", - "type": "varchar(255)", - "primaryKey": false, - "notNull": true - }, - "description": { - "name": "description", - "type": "text", - "primaryKey": false, - "notNull": false - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - } - }, - "indexes": {}, - "foreignKeys": {}, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "role_id_unique": { - "name": "role_id_unique", - "nullsNotDistinct": false, - "columns": [ - "id" - ] - }, - "role_name_unique": { - "name": "role_name_unique", - "nullsNotDistinct": false, - "columns": [ - "name" - ] - } - } - }, - "public.stats_resources": { - "name": "stats_resources", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "resource_id": { - "name": "resource_id", - "type": "bigint", - "primaryKey": false, - "notNull": true - }, - "views": { - "name": "views", - "type": "bigint", - "primaryKey": false, - "notNull": true, - "default": 0 - }, - "downloads": { - "name": "downloads", - "type": "bigint", - "primaryKey": false, - "notNull": true, - "default": 0 - }, - "shares": { - "name": "shares", - "type": "bigint", - "primaryKey": false, - "notNull": true, - "default": 0 - }, - "score": { - "name": "score", - "type": "bigint", - "primaryKey": false, - "notNull": true, - "default": 0 - } - }, - "indexes": {}, - "foreignKeys": { - "stats_resources_resource_id_resource_id_fk": { - "name": "stats_resources_resource_id_resource_id_fk", - "tableFrom": "stats_resources", - "tableTo": "resource", - "columnsFrom": [ - "resource_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "stats_resources_id_unique": { - "name": "stats_resources_id_unique", - "nullsNotDistinct": false, - "columns": [ - "id" - ] - } - } - }, - "public.subjects": { - "name": "subjects", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "name": { - "name": "name", - "type": "varchar", - "primaryKey": false, - "notNull": true - } - }, - "indexes": {}, - "foreignKeys": {}, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "subjects_id_unique": { - "name": "subjects_id_unique", - "nullsNotDistinct": false, - "columns": [ - "id" - ] - }, - "subjects_name_unique": { - "name": "subjects_name_unique", - "nullsNotDistinct": false, - "columns": [ - "name" - ] - } - } - }, - "public.submission": { - "name": "submission", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "is_accepted": { - "name": "is_accepted", - "type": "boolean", - "primaryKey": false, - "notNull": true, - "default": false - }, - "justification": { - "name": "justification", - "type": "text", - "primaryKey": false, - "notNull": false - }, - "resource_id": { - "name": "resource_id", - "type": "integer", - "primaryKey": false, - "notNull": true - }, - "submitter_id": { - "name": "submitter_id", - "type": "integer", - "primaryKey": false, - "notNull": true - }, - "curator_id": { - "name": "curator_id", - "type": "integer", - "primaryKey": false, - "notNull": true - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": false - }, - "answered_at": { - "name": "answered_at", - "type": "timestamp", - "primaryKey": false, - "notNull": false - }, - "q1": { - "name": "q1", - "type": "boolean", - "primaryKey": false, - "notNull": false - }, - "q2": { - "name": "q2", - "type": "boolean", - "primaryKey": false, - "notNull": false - }, - "q3": { - "name": "q3", - "type": "boolean", - "primaryKey": false, - "notNull": false - }, - "q4": { - "name": "q4", - "type": "boolean", - "primaryKey": false, - "notNull": false - } - }, - "indexes": {}, - "foreignKeys": { - "submission_resource_id_resource_id_fk": { - "name": "submission_resource_id_resource_id_fk", - "tableFrom": "submission", - "tableTo": "resource", - "columnsFrom": [ - "resource_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - }, - "submission_submitter_id_user_id_fk": { - "name": "submission_submitter_id_user_id_fk", - "tableFrom": "submission", - "tableTo": "user", - "columnsFrom": [ - "submitter_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - }, - "submission_curator_id_user_id_fk": { - "name": "submission_curator_id_user_id_fk", - "tableFrom": "submission", - "tableTo": "user", - "columnsFrom": [ - "curator_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": {} - }, - "public.user_role": { - "name": "user_role", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "user_id": { - "name": "user_id", - "type": "integer", - "primaryKey": false, - "notNull": true - }, - "role_id": { - "name": "role_id", - "type": "integer", - "primaryKey": false, - "notNull": true - } - }, - "indexes": {}, - "foreignKeys": { - "user_role_user_id_user_id_fk": { - "name": "user_role_user_id_user_id_fk", - "tableFrom": "user_role", - "tableTo": "user", - "columnsFrom": [ - "user_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - }, - "user_role_role_id_role_id_fk": { - "name": "user_role_role_id_role_id_fk", - "tableFrom": "user_role", - "tableTo": "role", - "columnsFrom": [ - "role_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "user_role_id_unique": { - "name": "user_role_id_unique", - "nullsNotDistinct": false, - "columns": [ - "id" - ] - } - } - }, - "public.user_stats": { - "name": "user_stats", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "user_id": { - "name": "user_id", - "type": "integer", - "primaryKey": false, - "notNull": true - }, - "score": { - "name": "score", - "type": "integer", - "primaryKey": false, - "notNull": true, - "default": 0 - }, - "likes": { - "name": "likes", - "type": "integer", - "primaryKey": false, - "notNull": true, - "default": 0 - }, - "likes_received": { - "name": "likes_received", - "type": "integer", - "primaryKey": false, - "notNull": true, - "default": 0 - }, - "follows": { - "name": "follows", - "type": "integer", - "primaryKey": false, - "notNull": true, - "default": 0 - }, - "followers": { - "name": "followers", - "type": "integer", - "primaryKey": false, - "notNull": true, - "default": 0 - }, - "collections": { - "name": "collections", - "type": "integer", - "primaryKey": false, - "notNull": true, - "default": 0 - }, - "submitted_resources": { - "name": "submitted_resources", - "type": "integer", - "primaryKey": false, - "notNull": true, - "default": 0 - }, - "approved_resources": { - "name": "approved_resources", - "type": "integer", - "primaryKey": false, - "notNull": true, - "default": 0 - }, - "reviewed_resources": { - "name": "reviewed_resources", - "type": "integer", - "primaryKey": false, - "notNull": true, - "default": 0 - }, - "comments": { - "name": "comments", - "type": "integer", - "primaryKey": false, - "notNull": true, - "default": 0 - } - }, - "indexes": {}, - "foreignKeys": { - "user_stats_user_id_user_id_fk": { - "name": "user_stats_user_id_user_id_fk", - "tableFrom": "user_stats", - "tableTo": "user", - "columnsFrom": [ - "user_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "user_stats_id_unique": { - "name": "user_stats_id_unique", - "nullsNotDistinct": false, - "columns": [ - "id" - ] - } - } - }, - "public.user": { - "name": "user", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "name": { - "name": "name", - "type": "varchar(255)", - "primaryKey": false, - "notNull": true - }, - "username": { - "name": "username", - "type": "varchar(255)", - "primaryKey": false, - "notNull": true - }, - "password": { - "name": "password", - "type": "varchar(255)", - "primaryKey": false, - "notNull": true - }, - "email": { - "name": "email", - "type": "varchar(255)", - "primaryKey": false, - "notNull": true - }, - "description": { - "name": "description", - "type": "text", - "primaryKey": false, - "notNull": false, - "default": "'sem descrição'" - }, - "institution": { - "name": "institution", - "type": "text", - "primaryKey": false, - "notNull": false, - "default": "'sem instituição'" - }, - "birthday": { - "name": "birthday", - "type": "timestamp", - "primaryKey": false, - "notNull": true - }, - "cpf": { - "name": "cpf", - "type": "varchar(255)", - "primaryKey": false, - "notNull": true - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "confirmed_at": { - "name": "confirmed_at", - "type": "timestamp", - "primaryKey": false, - "notNull": false - }, - "confirmation_sent_at": { - "name": "confirmation_sent_at", - "type": "timestamp", - "primaryKey": false, - "notNull": false - }, - "deleted_at": { - "name": "deleted_at", - "type": "timestamp", - "primaryKey": false, - "notNull": false - }, - "reactivated_at": { - "name": "reactivated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": false - }, - "active": { - "name": "active", - "type": "boolean", - "primaryKey": false, - "notNull": false, - "default": true - } - }, - "indexes": {}, - "foreignKeys": {}, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "user_id_unique": { - "name": "user_id_unique", - "nullsNotDistinct": false, - "columns": [ - "id" - ] - }, - "user_username_unique": { - "name": "user_username_unique", - "nullsNotDistinct": false, - "columns": [ - "username" - ] - }, - "user_email_unique": { - "name": "user_email_unique", - "nullsNotDistinct": false, - "columns": [ - "email" - ] - } - } - } - }, - "enums": {}, - "schemas": {}, - "_meta": { - "columns": {}, - "schemas": {}, - "tables": {} - } -} \ No newline at end of file diff --git a/src/db/migrations/meta/_journal.json b/src/db/migrations/meta/_journal.json index 255a3282bc4df65f886b581676802cef36b6beaa..6c380b43155c81440963a530b41839dc9068c2f1 100644 --- a/src/db/migrations/meta/_journal.json +++ b/src/db/migrations/meta/_journal.json @@ -5,15 +5,8 @@ { "idx": 0, "version": "7", - "when": 1729264247365, - "tag": "0000_lyrical_silver_samurai", - "breakpoints": true - }, - { - "idx": 1, - "version": "7", - "when": 1729512790298, - "tag": "0001_eager_living_lightning", + "when": 1729522512009, + "tag": "0000_narrow_payback", "breakpoints": true } diff --git a/src/db/repo/achievement.repo.ts b/src/db/repo/achievement.repo.ts new file mode 100644 index 0000000000000000000000000000000000000000..4e700700125fb1823399996ae536b852fa4581dc --- /dev/null +++ b/src/db/repo/achievement.repo.ts @@ -0,0 +1,56 @@ +import { Service } from "typedi"; +import type { AchievementInput, AchievementModel, AchievementUpdate } from "../schema/achievements.schema"; +import db from ".."; +import achievementTable, { achievementSchemas } from "../schema/achievements.schema"; +import { eq } from "drizzle-orm"; + +@Service() +export class AchievementRepo { + async create(achievement: AchievementInput): Promise<AchievementModel> { + const [ret] = await db + .insert(achievementTable) + .values(achievement) + .returning() + + return achievementSchemas.achievementModelSchema.parse(ret) + } + + async update(achievement: AchievementUpdate): Promise<AchievementModel> { + achievement.updated_at = new Date().toISOString() + + const [ret] = await db + .update(achievementTable) + .set(achievement) + .where(eq(achievementTable.id, achievement.id)) + .returning() + + return achievementSchemas.achievementModelSchema.parse(ret) + } + + async delete(id: AchievementModel['id']): Promise<AchievementModel> { + const [ret] = await db + .delete(achievementTable) + .where(eq(achievementTable.id, id)) + .returning() + + return achievementSchemas.achievementModelSchema.parse(ret) + } + + async find( + id: AchievementModel['id'] + ): Promise<AchievementModel | undefined> { + const achievement = await db.query.achievementTable.findFirst({ + where: eq(achievementTable.id, id) + }) + + if(!achievement) return undefined + + return achievementSchemas.achievementModelSchema.parse(achievement) + } + + async findMany(): Promise<AchievementModel[]> { + return achievementSchemas.achievementModelSchema + .array() + .parse(await db.query.achievementTable.findMany()) + } +} \ No newline at end of file diff --git a/src/db/schema/achievements.schema.ts b/src/db/schema/achievements.schema.ts new file mode 100644 index 0000000000000000000000000000000000000000..b78896d42ae80007780cf417f68f76a48b56675d --- /dev/null +++ b/src/db/schema/achievements.schema.ts @@ -0,0 +1,39 @@ +import { boolean, integer, numeric, pgEnum, pgTable, serial, text, timestamp, varchar } from "drizzle-orm/pg-core"; +import { createInsertSchema, createSelectSchema } from "drizzle-zod"; +import { z } from "zod"; + +const stateEnum = pgEnum('state', ['active', 'inactive', 'under_review']) + +const achievementTable = pgTable('achievement', { + id: serial('id').primaryKey().notNull().unique(), + name: varchar('name', { length: 255 }).notNull(), + description: text('description'), + reward_experience: numeric('reward_experience'), + reward_points: numeric('reward_points'), + // o campo state foi comentado pois a criação do tipo enum terá que ser feita manualmente na migração + //state: stateEnum('state').notNull().default('inactive'), + state: integer('state').notNull().default(0), + repeatable: integer('repeatable').notNull(), + is_resettable: boolean('is_resettable').notNull(), + created_at: timestamp('created_at', { mode: 'string' }).notNull().defaultNow(), + updated_at: timestamp('updated_at', { mode: 'string' }).notNull().defaultNow(), +}) + +const achievementModelSchema = createSelectSchema(achievementTable) +const achievementDtoSchema = achievementModelSchema +const achievementInputSchema = createInsertSchema(achievementTable) +const achievementUpdateSchema = achievementInputSchema.partial().required({ id: true }) + +export type AchievementModel = z.infer<typeof achievementModelSchema> +export type AchievementDto = z.infer<typeof achievementDtoSchema> +export type AchievementInput = z.infer<typeof achievementInputSchema> +export type AchievementUpdate = z.infer<typeof achievementUpdateSchema> + +export const achievementSchemas = { + achievementModelSchema, + achievementDtoSchema, + achievementInputSchema, + achievementUpdateSchema, +} + +export default achievementTable \ No newline at end of file diff --git a/src/db/schema/index.ts b/src/db/schema/index.ts index d0bbac5c2fed92bdfadf43002c504b150946f879..8c20b59eb4d364b6f0de2ca3496e11d49f68600d 100644 --- a/src/db/schema/index.ts +++ b/src/db/schema/index.ts @@ -15,6 +15,7 @@ import userTable from './user.schema' import objectTypeTable from './object-type.schema' import complaintTable from './complaints.schema' import resourceSubjectsTable from './resource-subjects.schema' +import achievementTable from './achievements.schema' import resourceLanguagesTable from './resource-languages.schema' import followRelationTable from '../relations/follows.relation' @@ -53,7 +54,8 @@ export { institutionTable, userInstitutionRelationTable, resourceLikesTable, - resourceEducationalStagesTable + resourceEducationalStagesTable, + achievementTable, } export const tables = [ @@ -79,4 +81,5 @@ export const tables = [ institutionTable, userInstitutionRelationTable, resourceLikesTable, + achievementTable, ] diff --git a/src/db/seed.ts b/src/db/seed.ts index c456c9f7ee5ac9d2e9ffcc84a21fd35187135a21..6b050d47fc2abcd5b1cea7482d9c8dd7bf3995b0 100644 --- a/src/db/seed.ts +++ b/src/db/seed.ts @@ -44,5 +44,6 @@ await seeds.resourceSubjectsSeed(db) await seeds.userInstitutionSeed(db) await seeds.resourceLikesSeed(db) await seeds.resourceEducationalStagesSeed(db) +await seeds.achievementSeed(db) await connection.end() diff --git a/src/db/seeds/achievement.seed.ts b/src/db/seeds/achievement.seed.ts new file mode 100644 index 0000000000000000000000000000000000000000..5ca8158abb7c1114b4f757ff1e7c8a7656d58bc8 --- /dev/null +++ b/src/db/seeds/achievement.seed.ts @@ -0,0 +1,38 @@ +import type db from ".."; +import { achievementTable } from "../schema"; +import type { AchievementInput } from "../schema/achievements.schema"; + +export default async function seed(db: db) { + await db.insert(achievementTable).values(achievementData) +} + +const achievementData: AchievementInput[] = [ + { + name: 'Achievement 1', + description: 'Achievement 1 description', + reward_experience: 100, + reward_points: 10, + state: 2, + repeatable: 5, + is_resettable: true, + }, + { + name: 'Achievement 2', + description: 'Achievement 2 description', + reward_experience: 200, + reward_points: 20, + state: 1, + repeatable: 5, + is_resettable: true, + }, + { + name: 'Achievement 3', + description: 'Achievement 3 description', + reward_experience: 300, + reward_points: 30, + state: 1, + repeatable: 5, + is_resettable: true, + } + +] \ No newline at end of file diff --git a/src/db/seeds/index.ts b/src/db/seeds/index.ts index f683802f6c80d800ab5c78bd19e1ebd3b2a54cdb..f3d21f7b29134e4585c87ab0cb577b014a3d8429 100644 --- a/src/db/seeds/index.ts +++ b/src/db/seeds/index.ts @@ -21,3 +21,4 @@ export { default as institutionSeed } from './institutions.seed' export { default as userInstitutionSeed } from './user-institution.seed' export { default as resourceLikesSeed } from './resource-likes.seed' export { default as resourceEducationalStagesSeed } from './resource-educational-stages.seed' +export { default as achievementSeed } from './achievement.seed' diff --git a/src/index.ts b/src/index.ts index f0cd40b9b056383a0fca59d9d4ef3151ea04db31..f9b8e17a768f6710386c7649f3aa55d33917ff63 100644 --- a/src/index.ts +++ b/src/index.ts @@ -33,6 +33,7 @@ import { publicResourceSubjectsRouter, resourceSubjectsRouter } from './routes/r import { userInstitutionRouter } from './routes/user-institution.route' import { publicResourceLikesRoutes, resourceLikesRoutes } from './routes/resource-likes.route' import { publicResourceEducationalStagesRouter, resourceEducationalStagesRouter } from './routes/resource-educational-stages.route' +import { achievementRouter, publicAchievementRouter } from './routes/achievement.route' const app = new Hono() @@ -89,6 +90,7 @@ app .route('/resourceEducationalStages', publicResourceEducationalStagesRouter) .route('/resourceLikes', publicResourceLikesRoutes) + .route('/achievements', publicAchievementRouter) //rotas que precisam de token app .basePath('/api') @@ -116,6 +118,7 @@ app .route('/user-institutions', userInstitutionRouter) .route('/resourceEducationalStages', resourceEducationalStagesRouter) + .route('/achievements', achievementRouter) .route('/resourceLikes', resourceLikesRoutes) export default app export type AppType = typeof app diff --git a/src/routes/achievement.route.ts b/src/routes/achievement.route.ts new file mode 100644 index 0000000000000000000000000000000000000000..c0351897bdba79f790f1d9a28b466addccb91c1b --- /dev/null +++ b/src/routes/achievement.route.ts @@ -0,0 +1,125 @@ +import { AchievementService } from "@/services/achievement.service"; +import Container, { ContainerInstance } from "typedi"; +import { honoWithJwt } from ".."; +import { zValidator } from "@hono/zod-validator"; +import { achievementSchemas } from "@/db/schema/achievements.schema"; +import { createApexError, HttpStatus } from "@/services/error.service"; +import { Hono } from "hono"; + +const service = Container.get(AchievementService) + +export const achievementRouter = honoWithJwt() + .post('/create', zValidator('json', achievementSchemas.achievementInputSchema), + async (c) => { + try { + const input = await c.req.valid('json') + + const achievement = achievementSchemas.achievementDtoSchema.parse( + await service.create(input) + ) + + return c.json({achievement}) + } catch (e) { + return c.json( + createApexError({ + status: 'error', + message: 'could not create achievement', + code: HttpStatus.BAD_REQUEST, + path: c.req.routePath, + suggestion: 'check the input and try again', + }), + HttpStatus.BAD_REQUEST + ) + } + } + ) + .post('/update', zValidator('json', achievementSchemas.achievementUpdateSchema), + async (c) => { + try { + const input = await c.req.valid('json') + + const achievement = achievementSchemas.achievementDtoSchema.parse( + await service.update(input) + ) + + return c.json({achievement}) + } catch (e) { + console.log(e) + + return c.json( + createApexError({ + status: 'error', + message: 'could not update achievement', + code: HttpStatus.BAD_REQUEST, + path: c.req.routePath, + suggestion: 'check the input and try again', + }), + HttpStatus.BAD_REQUEST + ) + } + } + ) + .post('/delete/:id', async (c) => { + try { + const id = +c.req.param('id') + + const achievement = achievementSchemas.achievementDtoSchema.parse( + await service.delete(id) + ) + + return c.json({achievement}) + } catch (e) { + return c.json( + createApexError({ + status: 'error', + message: 'could not delete achievement', + code: HttpStatus.BAD_REQUEST, + path: c.req.routePath, + suggestion: 'check the input and try again', + }), + HttpStatus.BAD_REQUEST + ) + } + }) + + export const publicAchievementRouter = new Hono() + .get('/:id', async (c) => { + try { + const id = +c.req.param('id') + + const achievement = achievementSchemas.achievementDtoSchema.parse( + await service.find(id) + ) + return c.json({achievement}) + } catch (e) { + return c.json( + createApexError({ + status: 'error', + message: 'could not find achievement', + code: HttpStatus.NOT_FOUND, + path: c.req.routePath, + suggestion: 'check the input and try again', + }), + HttpStatus.NOT_FOUND + ) + } + }) + .get('/', async (c) => { + try { + const achievements = achievementSchemas.achievementDtoSchema.array().parse( + await service.findMany() + ) + return c.json({achievements}) + } catch (e) { + return c.json( + createApexError({ + status: 'error', + message: 'could not find achievements', + code: HttpStatus.NOT_FOUND, + path: c.req.routePath, + suggestion: 'check the input and try again', + }), + HttpStatus.NOT_FOUND + ) + } + }) \ No newline at end of file diff --git a/src/services/achievement.service.ts b/src/services/achievement.service.ts new file mode 100644 index 0000000000000000000000000000000000000000..fb28f6280109c583413d9b589eeb7ad8d97db81f --- /dev/null +++ b/src/services/achievement.service.ts @@ -0,0 +1,29 @@ +import { AchievementRepo } from "@/db/repo/achievement.repo"; +import type { AchievementInput, AchievementModel, AchievementUpdate } from "@/db/schema/achievements.schema"; +import { Inject, Service } from "typedi"; + +@Service() +export class AchievementService { + @Inject() + private readonly repo: AchievementRepo; + + async create(achievement: AchievementInput): Promise<AchievementModel> { + return this.repo.create(achievement) + } + + async update(achievement: AchievementUpdate): Promise<AchievementModel> { + return this.repo.update(achievement) + } + + async delete(id: AchievementModel['id']): Promise<AchievementModel> { + return this.repo.delete(id) + } + + async find(id: AchievementModel['id']): Promise<AchievementModel | undefined> { + return this.repo.find(id) + } + + async findMany(): Promise<AchievementModel[]> { + return this.repo.findMany() + } +} \ No newline at end of file