Skip to content

Commit

Permalink
Merge pull request #9 from Webster-FR/feature/tasks
Browse files Browse the repository at this point in the history
Merge tasks to Main
  • Loading branch information
Xen0Xys authored Jan 6, 2024
2 parents 9c8cf31 + e757c62 commit 2428d72
Show file tree
Hide file tree
Showing 22 changed files with 481 additions and 774 deletions.
674 changes: 0 additions & 674 deletions LICENSE

This file was deleted.

157 changes: 157 additions & 0 deletions LICENSE.md

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0a",
"description": "This is a basic template for building a RESTful API using Node.js and NestJS. It provides a structured starting point for your API development, with essential components and best practices.",
"author": "Xen0Xys",
"license": "GNU General Public License v3.0",
"license": "CC-BY-NC-ND-4.0",
"repository": {
"type": "git",
"url": "git+https://github.com/Xen0Xys/ts-nest-api-template.git"
Expand Down Expand Up @@ -33,6 +33,7 @@
"@nestjs/config": "^3.1.1",
"@nestjs/core": "^10.3.0",
"@nestjs/platform-fastify": "^10.3.0",
"@nestjs/schedule": "^4.0.0",
"@nestjs/swagger": "^7.1.17",
"@prisma/client": "5.7.1",
"argon2": "^0.31.2",
Expand Down
38 changes: 38 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ CREATE TABLE "users" (
"email" TEXT NOT NULL,
"password" TEXT NOT NULL,
"secret" TEXT NOT NULL,
"verification_code_id" INTEGER,
"created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" DATETIME NOT NULL,
CONSTRAINT "users_verification_code_id_fkey" FOREIGN KEY ("verification_code_id") REFERENCES "verification_codes" ("id") ON DELETE SET NULL ON UPDATE CASCADE
"updated_at" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "verification_codes" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"user_id" INTEGER NOT NULL,
"code" TEXT NOT NULL,
"iat" DATETIME NOT NULL
"iat" DATETIME NOT NULL,
CONSTRAINT "verification_codes_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
Expand All @@ -27,7 +27,7 @@ CREATE TABLE "tokens" (
"is_refresh" BOOLEAN NOT NULL,
"expires" DATETIME NOT NULL,
"blacklisted" BOOLEAN NOT NULL DEFAULT false,
CONSTRAINT "tokens_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
CONSTRAINT "tokens_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
Expand All @@ -43,7 +43,7 @@ CREATE TABLE "todos" (
"color" TEXT NOT NULL,
"created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" DATETIME NOT NULL,
CONSTRAINT "todos_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "todos_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "todos_parent_id_fkey" FOREIGN KEY ("parent_id") REFERENCES "todos" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);

Expand All @@ -60,7 +60,7 @@ CREATE TABLE "banks" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL,
"user_id" INTEGER,
CONSTRAINT "banks_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE SET NULL ON UPDATE CASCADE
CONSTRAINT "banks_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
Expand All @@ -71,7 +71,7 @@ CREATE TABLE "accounts" (
"bank_id" INTEGER NOT NULL,
"user_id" INTEGER NOT NULL,
CONSTRAINT "accounts_bank_id_fkey" FOREIGN KEY ("bank_id") REFERENCES "banks" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "accounts_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
CONSTRAINT "accounts_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
Expand All @@ -88,7 +88,7 @@ CREATE TABLE "recurring_transactions" (
"to_account_id" INTEGER,
"created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" DATETIME NOT NULL,
CONSTRAINT "recurring_transactions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "recurring_transactions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "recurring_transactions_category_id_fkey" FOREIGN KEY ("category_id") REFERENCES "transaction_categories" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT "recurring_transactions_from_account_id_fkey" FOREIGN KEY ("from_account_id") REFERENCES "accounts" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT "recurring_transactions_to_account_id_fkey" FOREIGN KEY ("to_account_id") REFERENCES "accounts" ("id") ON DELETE SET NULL ON UPDATE CASCADE
Expand All @@ -101,7 +101,7 @@ CREATE TABLE "transaction_categories" (
"name" TEXT NOT NULL,
"icon" TEXT NOT NULL,
"color" TEXT NOT NULL,
CONSTRAINT "transaction_categories_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
CONSTRAINT "transaction_categories_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
Expand Down Expand Up @@ -168,7 +168,7 @@ CREATE TABLE "income_transactions" (
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");

-- CreateIndex
CREATE UNIQUE INDEX "users_verification_code_id_key" ON "users"("verification_code_id");
CREATE UNIQUE INDEX "verification_codes_user_id_key" ON "verification_codes"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "verification_codes_code_key" ON "verification_codes"("code");
Expand Down
24 changes: 12 additions & 12 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ model User {
email String @unique
password String
secret String
verification_code_id Int? @unique
created_at DateTime @default(now())
updated_at DateTime @updatedAt
verification_codes VerificationCodes? @relation(fields: [verification_code_id], references: [id])
verification_codes VerificationCodes?
tokens Tokens[]
todos Todos[]
banks Banks[]
Expand All @@ -31,10 +30,11 @@ model User {
}

model VerificationCodes {
id Int @id @default(autoincrement())
code String @unique
iat DateTime
user User? @relation()
id Int @id @default(autoincrement())
user_id Int @unique
code String @unique
iat DateTime
user User? @relation(fields: [user_id], references: [id], onDelete: Cascade)
@@map("verification_codes")
}
Expand All @@ -47,7 +47,7 @@ model Tokens {
is_refresh Boolean
expires DateTime
blacklisted Boolean @default(false)
user User @relation(fields: [user_id], references: [id])
user User @relation(fields: [user_id], references: [id], onDelete: Cascade)
@@map("tokens")
}
Expand All @@ -64,7 +64,7 @@ model Todos {
color String
created_at DateTime @default(now())
updated_at DateTime @updatedAt
user User @relation(fields: [user_id], references: [id])
user User @relation(fields: [user_id], references: [id], onDelete: Cascade)
parent Todos? @relation("child_todos", fields: [parent_id], references: [id])
todos Todos[] @relation("child_todos")
Expand All @@ -84,7 +84,7 @@ model Banks {
id Int @id @default(autoincrement())
name String
user_id Int?
user User? @relation(fields: [user_id], references: [id])
user User? @relation(fields: [user_id], references: [id], onDelete: Cascade)
account Accounts[]
@@unique([name, user_id])
Expand All @@ -98,7 +98,7 @@ model Accounts {
bank_id Int
user_id Int
bank Banks @relation(fields: [bank_id], references: [id])
user User @relation(fields: [user_id], references: [id])
user User @relation(fields: [user_id], references: [id], onDelete: Cascade)
from_recurring_transactions RecurringTransaction[] @relation("from_account")
to_recurring_transactions RecurringTransaction[] @relation("to_account")
internal_ledger InternalLedger[]
Expand All @@ -119,7 +119,7 @@ model RecurringTransaction {
to_account_id Int?
created_at DateTime @default(now())
updated_at DateTime @updatedAt
user User @relation(fields: [user_id], references: [id])
user User @relation(fields: [user_id], references: [id], onDelete: Cascade)
category TransactionCategories? @relation(fields: [category_id], references: [id])
from_account Accounts? @relation("from_account", fields: [from_account_id], references: [id])
to_account Accounts? @relation("to_account", fields: [to_account_id], references: [id])
Expand All @@ -133,7 +133,7 @@ model TransactionCategories {
name String
icon String
color String
user User @relation(fields: [user_id], references: [id])
user User @relation(fields: [user_id], references: [id], onDelete: Cascade)
recurring_transactions RecurringTransaction[]
internal_transactions InternalTransactions[]
expense_transactions ExpenseTransactions[]
Expand Down
1 change: 0 additions & 1 deletion prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ async function main(){
email: "test@exemple.org",
password: await encryptionService.hash("password"),
secret: encryptionService.encryptSymmetric(userSecret, encryptionKey),
verification_code_id: null,
created_at: new Date(),
updated_at: new Date(),
},
Expand Down
18 changes: 17 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,24 @@ import {TodosModule} from "./todos/todos.module";
import {TipsModule} from "./tips/tips.module";
import {MaintenanceModule} from "./maintenance/maintenance.module";
import {BanksModule} from "./banks/banks.module";
import {ScheduleModule} from "@nestjs/schedule";
import {TasksModule} from "./tasks/tasks.module";
import {SecretsModule} from "./secrets/secrets.module";

@Module({
imports: [AuthModule, VersionModule, ConfigModule.forRoot({isGlobal: true}), UsersModule, VerificationCodesModule, TodosModule, TipsModule, MaintenanceModule, BanksModule],
imports: [
ConfigModule.forRoot({isGlobal: true}),
ScheduleModule.forRoot(),
AuthModule,
VersionModule,
UsersModule,
VerificationCodesModule,
TodosModule,
TipsModule,
MaintenanceModule,
BanksModule,
TasksModule,
SecretsModule,
],
})
export class AppModule{}
19 changes: 11 additions & 8 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
BadRequestException,
ConflictException,
ForbiddenException,
Injectable,
Injectable, InternalServerErrorException,
NotFoundException, UnauthorizedException
} from "@nestjs/common";
import {EncryptionService} from "../services/encryption.service";
Expand All @@ -29,11 +29,12 @@ export class AuthService{
const user = await this.usersService.findByEmail(email);
if(!await this.encryptionService.compareHash(user.password, password))
throw new UnauthorizedException("Invalid password");
if(user.verification_code_id){
if(!await this.verificationCodeService.checkCodeValidity(user.verification_code_id)){
const newCode = await this.verificationCodeService.generateNewCode(user.verification_code_id);
await this.usersService.setVerificationCode(user.id, newCode.id);
await this.emailService.sendConfirmationEmail(user.email, newCode.code);
const code = await this.verificationCodeService.findByUserId(user.id);
if(code){
if(!await this.verificationCodeService.checkCodeValidity(code)){
const newCode = this.encryptionService.generateSecret();
await this.verificationCodeService.setCode(user.id, newCode);
await this.emailService.sendConfirmationEmail(user.email, newCode);
return null;
}
throw new ForbiddenException("Email not confirmed");
Expand Down Expand Up @@ -61,7 +62,9 @@ export class AuthService{
if(user)
throw new ConflictException("Email already exists");
const newUser = await this.usersService.createUser(username, email, password);
const code = await this.verificationCodeService.findById(newUser.verification_code_id);
const code = await this.verificationCodeService.findByUserId(newUser.id);
if(!code)
throw new InternalServerErrorException("Verification code not found");
await this.emailService.sendConfirmationEmail(newUser.email, code.code);
}

Expand All @@ -85,7 +88,7 @@ export class AuthService{
const user = await this.usersService.findByVerificationCode(verificationCode.id);
if(!user)
throw new NotFoundException("No user found for this code");
if(!await this.verificationCodeService.checkCode(verificationCode.id, requestCode))
if(!await this.verificationCodeService.checkCode(verificationCode, requestCode))
throw new BadRequestException("Invalid code");
await this.verificationCodeService.deleteById(verificationCode.id);
}
Expand Down
12 changes: 12 additions & 0 deletions src/secrets/secrets.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {Module} from "@nestjs/common";
import {SecretsService} from "./secrets.service";
import {ServicesModule} from "../services/services.module";
import {UsersModule} from "../users/users.module";
import {TodosModule} from "../todos/todos.module";

@Module({
providers: [SecretsService],
imports: [ServicesModule, UsersModule, TodosModule],
exports: [SecretsService],
})
export class SecretsModule{}
Loading

0 comments on commit 2428d72

Please sign in to comment.