diff --git a/docker-compose.yml b/docker-compose.yml index 528161e..62ab383 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,14 +2,14 @@ version: "3.8" services: postgres: - container_name: nest-clean-pg + container_name: fast-feet-pg image: bitnami/postgresql ports: - 5432:5432 environment: - POSTGRESQL_USERNAME=postgres - POSTGRESQL_PASSWORD=docker - - POSTGRESQL_DATABASE=nest-clean + - POSTGRESQL_DATABASE=fast-feet - PG_DATA=/data/postgres volumes: - ./data/pg:/data/postgres diff --git a/package.json b/package.json index e5e4ce5..3ec1cd6 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "private": true, "license": "UNLICENSED", "scripts": { + "db": "prisma studio", "build": "nest build", "start": "nest start", "dev": "nest start --watch", diff --git a/prisma/migrations/20240618211748_create_database_structure/migration.sql b/prisma/migrations/20240618211748_create_database_structure/migration.sql new file mode 100644 index 0000000..94a5a17 --- /dev/null +++ b/prisma/migrations/20240618211748_create_database_structure/migration.sql @@ -0,0 +1,61 @@ +-- CreateEnum +CREATE TYPE "UserRoles" AS ENUM ('recipient', 'adm', 'courier'); + +-- CreateEnum +CREATE TYPE "UpdateObjectTypes" AS ENUM ('recipient', 'adm', 'courier', 'order'); + +-- CreateTable +CREATE TABLE "orders" ( + "id" TEXT NOT NULL, + "recipient_d" TEXT NOT NULL, + "courier_id" TEXT, + "address" JSONB NOT NULL, + "delivered" TIMESTAMP(3), + "delivered_photo" JSONB, + "awaiting_pickup" TIMESTAMP(3), + "collected" TIMESTAMP(3), + "returned" TIMESTAMP(3), + "return_cause" TEXT, + "created_at" TIMESTAMP(3) NOT NULL, + "updated_at" TIMESTAMP(3), + + CONSTRAINT "orders_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "users" ( + "id" TEXT NOT NULL, + "name" TEXT NOT NULL, + "cpf" TEXT NOT NULL, + "password" TEXT NOT NULL, + "role" "UserRoles" NOT NULL, + "coordinates" JSONB, + "created_at" TIMESTAMP(3) NOT NULL, + "updated_at" TIMESTAMP(3), + + CONSTRAINT "users_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "updates" ( + "id" TEXT NOT NULL, + "object_id" TEXT NOT NULL, + "updated_by" TEXT NOT NULL, + "date" TIMESTAMP(3) NOT NULL, + "changes" JSONB NOT NULL, + "object_type" "UpdateObjectTypes" NOT NULL, + + CONSTRAINT "updates_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "users_cpf_key" ON "users"("cpf"); + +-- AddForeignKey +ALTER TABLE "orders" ADD CONSTRAINT "orders_recipient_d_fkey" FOREIGN KEY ("recipient_d") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "orders" ADD CONSTRAINT "orders_courier_id_fkey" FOREIGN KEY ("courier_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "updates" ADD CONSTRAINT "updates_updated_by_fkey" FOREIGN KEY ("updated_by") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/migrations/20240618212407_create_attachments/migration.sql b/prisma/migrations/20240618212407_create_attachments/migration.sql new file mode 100644 index 0000000..90d9ffd --- /dev/null +++ b/prisma/migrations/20240618212407_create_attachments/migration.sql @@ -0,0 +1,12 @@ +-- CreateTable +CREATE TABLE "PrismaAttachments" ( + "id" TEXT NOT NULL, + "title" TEXT NOT NULL, + "url" TEXT NOT NULL, + "orderId" TEXT, + + CONSTRAINT "PrismaAttachments_pkey" PRIMARY KEY ("id") +); + +-- AddForeignKey +ALTER TABLE "PrismaAttachments" ADD CONSTRAINT "PrismaAttachments_orderId_fkey" FOREIGN KEY ("orderId") REFERENCES "orders"("id") ON DELETE SET NULL ON UPDATE CASCADE; diff --git a/prisma/migrations/20240618230134_update_json_fields_to_string_fields/migration.sql b/prisma/migrations/20240618230134_update_json_fields_to_string_fields/migration.sql new file mode 100644 index 0000000..5be6e4c --- /dev/null +++ b/prisma/migrations/20240618230134_update_json_fields_to_string_fields/migration.sql @@ -0,0 +1,9 @@ +-- AlterTable +ALTER TABLE "orders" ALTER COLUMN "address" SET DATA TYPE TEXT, +ALTER COLUMN "delivered_photo" SET DATA TYPE TEXT; + +-- AlterTable +ALTER TABLE "updates" ALTER COLUMN "changes" SET DATA TYPE TEXT; + +-- AlterTable +ALTER TABLE "users" ALTER COLUMN "coordinates" SET DATA TYPE TEXT; diff --git a/prisma/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml new file mode 100644 index 0000000..fbffa92 --- /dev/null +++ b/prisma/migrations/migration_lock.toml @@ -0,0 +1,3 @@ +# Please do not edit this file manually +# It should be added in your version-control system (i.e. Git) +provider = "postgresql" \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma new file mode 100644 index 0000000..9bf03fe --- /dev/null +++ b/prisma/schema.prisma @@ -0,0 +1,87 @@ +// This is your Prisma schema file, +// learn more about it in the docs: https://pris.ly/d/prisma-schema + +// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions? +// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init + +generator client { + provider = "prisma-client-js" +} + +datasource db { + provider = "postgresql" + url = env("DATABASE_URL") +} + +model PrismaOrder { + id String @id @default(uuid()) + recipientId String @map("recipient_d") + courierId String? @map("courier_id") + address String + delivered DateTime? + deliveredPhoto String? @map("delivered_photo") + awaitingPickup DateTime? @map("awaiting_pickup") + collected DateTime? + returned DateTime? + returnCause String? @map("return_cause") + createdAt DateTime @map("created_at") + updatedAt DateTime? @map("updated_at") + + recipient PrismaUser @relation("recipient", fields: [recipientId], references: [id]) + courier PrismaUser? @relation("courier", fields: [courierId], references: [id]) + attachments PrismaAttachments[] + + @@map("orders") +} + +enum UserRoles { + recipient + adm + courier +} + +model PrismaUser { + id String @id @default(uuid()) + name String + cpf String @unique + password String + role UserRoles + coordinates String? + createdAt DateTime @map("created_at") + updatedAt DateTime? @map("updated_at") + + recipient PrismaOrder[] @relation("recipient") + courier PrismaOrder[] @relation("courier") + updates PrismaUpdates[] + + @@map("users") +} + +enum UpdateObjectTypes { + recipient + adm + courier + order +} + +model PrismaUpdates { + id String @id @default(uuid()) + objectId String @map("object_id") + updatedBy String @map("updated_by") + date DateTime + changes String + objectType UpdateObjectTypes @map("object_type") + + user PrismaUser @relation(fields: [updatedBy], references: [id]) + + @@map("updates") +} + +model PrismaAttachments { + id String @id @default(uuid()) + title String + url String + orderId String? + + order PrismaOrder? @relation(fields: [orderId], references: [id]) +} diff --git a/src/core/errors/errors/entity-type-error.ts b/src/core/errors/errors/entity-type-error.ts new file mode 100644 index 0000000..6ecd568 --- /dev/null +++ b/src/core/errors/errors/entity-type-error.ts @@ -0,0 +1,7 @@ +import { UseCaseError } from '@/core/errors/use-case-errors' + +export class EntityTypeError extends Error implements UseCaseError { + constructor(message: string) { + super(message) + } +} diff --git a/src/domain/core/deliveries-and-orders/application/repositories/adm-repositories/adm-updates-repository.ts b/src/domain/core/deliveries-and-orders/application/repositories/adm-repositories/adm-updates-repository.ts index 713c795..736a681 100644 --- a/src/domain/core/deliveries-and-orders/application/repositories/adm-repositories/adm-updates-repository.ts +++ b/src/domain/core/deliveries-and-orders/application/repositories/adm-repositories/adm-updates-repository.ts @@ -1,7 +1,5 @@ -import { Injectable } from '@nestjs/common' import { UpdateAdm } from '../../../enterprise/entities/update-adm' -@Injectable() export abstract class AdmUpdatesRepository { abstract create(update: UpdateAdm): Promise abstract update(update: UpdateAdm): Promise diff --git a/src/domain/core/deliveries-and-orders/application/repositories/adm-repositories/adms-repository.ts b/src/domain/core/deliveries-and-orders/application/repositories/adm-repositories/adms-repository.ts index afec6fd..ea126ee 100644 --- a/src/domain/core/deliveries-and-orders/application/repositories/adm-repositories/adms-repository.ts +++ b/src/domain/core/deliveries-and-orders/application/repositories/adm-repositories/adms-repository.ts @@ -1,7 +1,5 @@ -import { Injectable } from '@nestjs/common' import { Adm } from '../../../enterprise/entities/adm' -@Injectable() export abstract class AdmsRepository { abstract create(adm: Adm): Promise abstract update(adm: Adm): Promise diff --git a/src/domain/core/deliveries-and-orders/application/repositories/attachments-repository.ts b/src/domain/core/deliveries-and-orders/application/repositories/attachments-repository.ts index 6fa89ae..c55b2f4 100644 --- a/src/domain/core/deliveries-and-orders/application/repositories/attachments-repository.ts +++ b/src/domain/core/deliveries-and-orders/application/repositories/attachments-repository.ts @@ -1,7 +1,5 @@ -import { Injectable } from '@nestjs/common' import { Attachment } from '../../enterprise/entities/attachment' -@Injectable() export abstract class AttachmentsRepository { abstract create(update: Attachment): Promise abstract findById(id: string): Promise diff --git a/src/domain/core/deliveries-and-orders/application/repositories/courier-repositories/courier-repository.ts b/src/domain/core/deliveries-and-orders/application/repositories/courier-repositories/courier-repository.ts index c980af7..b484543 100644 --- a/src/domain/core/deliveries-and-orders/application/repositories/courier-repositories/courier-repository.ts +++ b/src/domain/core/deliveries-and-orders/application/repositories/courier-repositories/courier-repository.ts @@ -1,7 +1,5 @@ -import { Injectable } from '@nestjs/common' import { Courier } from '../../../enterprise/entities/courier' -@Injectable() export abstract class CouriersRepository { abstract create(courier: Courier): Promise abstract update(courier: Courier): Promise diff --git a/src/domain/core/deliveries-and-orders/application/repositories/courier-repositories/courier-updates-repository.ts b/src/domain/core/deliveries-and-orders/application/repositories/courier-repositories/courier-updates-repository.ts index 6a7614b..34c5909 100644 --- a/src/domain/core/deliveries-and-orders/application/repositories/courier-repositories/courier-updates-repository.ts +++ b/src/domain/core/deliveries-and-orders/application/repositories/courier-repositories/courier-updates-repository.ts @@ -1,7 +1,5 @@ -import { Injectable } from '@nestjs/common' import { UpdateCourier } from '../../../enterprise/entities/update-courier' -@Injectable() export abstract class CourierUpdatesRepository { abstract create(update: UpdateCourier): Promise abstract update(update: UpdateCourier): Promise diff --git a/src/domain/core/deliveries-and-orders/application/repositories/order-repositories/order-attachments-repository.ts b/src/domain/core/deliveries-and-orders/application/repositories/order-repositories/order-attachments-repository.ts index b5bf82d..dae2f8a 100644 --- a/src/domain/core/deliveries-and-orders/application/repositories/order-repositories/order-attachments-repository.ts +++ b/src/domain/core/deliveries-and-orders/application/repositories/order-repositories/order-attachments-repository.ts @@ -1,7 +1,5 @@ -import { Injectable } from '@nestjs/common' import { OrderAttachment } from '../../../enterprise/entities/order-attachment' -@Injectable() export abstract class OrderAttachmentsRepository { abstract create(update: OrderAttachment): Promise abstract findByOrderId(orderId: string): Promise diff --git a/src/domain/core/deliveries-and-orders/application/repositories/order-repositories/order-updates-repository.ts b/src/domain/core/deliveries-and-orders/application/repositories/order-repositories/order-updates-repository.ts index 6ecdd7c..f019935 100644 --- a/src/domain/core/deliveries-and-orders/application/repositories/order-repositories/order-updates-repository.ts +++ b/src/domain/core/deliveries-and-orders/application/repositories/order-repositories/order-updates-repository.ts @@ -1,7 +1,5 @@ -import { Injectable } from '@nestjs/common' import { UpdateOrder } from '../../../enterprise/entities/update-order' -@Injectable() export abstract class OrderUpdatesRepository { abstract create(update: UpdateOrder): Promise abstract update(update: UpdateOrder): Promise diff --git a/src/domain/core/deliveries-and-orders/application/repositories/order-repositories/orders-repository.ts b/src/domain/core/deliveries-and-orders/application/repositories/order-repositories/orders-repository.ts index 3dd84c1..3eb35fc 100644 --- a/src/domain/core/deliveries-and-orders/application/repositories/order-repositories/orders-repository.ts +++ b/src/domain/core/deliveries-and-orders/application/repositories/order-repositories/orders-repository.ts @@ -1,8 +1,6 @@ -import { Injectable } from '@nestjs/common' import { Order } from '../../../enterprise/entities/order' import { Coordinates } from '../../../enterprise/entities/value-objects/coordinates' -@Injectable() export abstract class OrdersRepository { abstract create(order: Order): Promise abstract update(order: Order): Promise diff --git a/src/domain/core/deliveries-and-orders/application/repositories/recipient-repositories/recipient-updates-repository.ts b/src/domain/core/deliveries-and-orders/application/repositories/recipient-repositories/recipient-updates-repository.ts index ec88bff..2f19fdb 100644 --- a/src/domain/core/deliveries-and-orders/application/repositories/recipient-repositories/recipient-updates-repository.ts +++ b/src/domain/core/deliveries-and-orders/application/repositories/recipient-repositories/recipient-updates-repository.ts @@ -1,7 +1,5 @@ -import { Injectable } from '@nestjs/common' import { UpdateRecipient } from '../../../enterprise/entities/update-recipient' -@Injectable() export abstract class RecipientUpdatesRepository { abstract create(update: UpdateRecipient): Promise abstract update(update: UpdateRecipient): Promise diff --git a/src/domain/core/deliveries-and-orders/application/repositories/recipient-repositories/recipients-repository.ts b/src/domain/core/deliveries-and-orders/application/repositories/recipient-repositories/recipients-repository.ts index 7f62bca..bd90108 100644 --- a/src/domain/core/deliveries-and-orders/application/repositories/recipient-repositories/recipients-repository.ts +++ b/src/domain/core/deliveries-and-orders/application/repositories/recipient-repositories/recipients-repository.ts @@ -1,7 +1,5 @@ -import { Injectable } from '@nestjs/common' import { Recipient } from '../../../enterprise/entities/recipient' -@Injectable() export abstract class RecipientsRepository { abstract create(recipient: Recipient): Promise abstract update(recipient: Recipient): Promise diff --git a/src/infra/database/database.module.ts b/src/infra/database/database.module.ts new file mode 100644 index 0000000..28ab8d2 --- /dev/null +++ b/src/infra/database/database.module.ts @@ -0,0 +1,8 @@ +import { Module } from '@nestjs/common' +import { PrismaService } from './prisma/prisma.service' +import { repositories } from './prisma/prisma.exports' + +@Module({ + providers: [PrismaService, ...repositories], +}) +export class DatabaseModule {} diff --git a/src/infra/database/prisma/mappers/prisma-adm-mapper.ts b/src/infra/database/prisma/mappers/prisma-adm-mapper.ts new file mode 100644 index 0000000..683d3d9 --- /dev/null +++ b/src/infra/database/prisma/mappers/prisma-adm-mapper.ts @@ -0,0 +1,58 @@ +import UniqueEntityId from '@/core/entities/unique-entity-id' +import { EntityTypeError } from '@/core/errors/errors/entity-type-error' +import { Adm } from '@/domain/core/deliveries-and-orders/enterprise/entities/adm' +import { + Coordinates, + CoordinatesProps, +} from '@/domain/core/deliveries-and-orders/enterprise/entities/value-objects/coordinates' +import { Cpf } from '@/domain/core/deliveries-and-orders/enterprise/entities/value-objects/cpf' +import { PrismaUser } from '@prisma/client' + +export class PrismaAdmMapper { + static toDomain(prismaAdm: PrismaUser): Adm { + if (prismaAdm.role !== 'adm') + throw new EntityTypeError( + `Adm.role must be equal "adm", but "${prismaAdm.role}" was passed`, + ) + + const coordinates = prismaAdm.coordinates + ? new Coordinates( + JSON.parse(prismaAdm.coordinates as string) as CoordinatesProps, + ) + : null + + const update = Adm.create( + { + cpf: new Cpf(prismaAdm.cpf), + name: prismaAdm.name, + password: prismaAdm.password, + role: prismaAdm.role, + coordinates, + createdAt: prismaAdm.createdAt, + updatedAt: prismaAdm.updatedAt, + }, + new UniqueEntityId(prismaAdm.id), + ) + + return update + } + + static domainToPrisma(adm: Adm): PrismaUser { + const coordinates = adm.coordinates?.raw + ? JSON.stringify(adm.coordinates.raw) + : null + + const prismaAdm: PrismaUser = { + id: adm.id.value, + cpf: adm.cpf.raw, + name: adm.name, + password: adm.password, + role: adm.role, + coordinates, + createdAt: adm.createdAt, + updatedAt: adm.updatedAt, + } + + return prismaAdm + } +} diff --git a/src/infra/database/prisma/mappers/prisma-attachment-mapper.ts b/src/infra/database/prisma/mappers/prisma-attachment-mapper.ts new file mode 100644 index 0000000..8ed56d9 --- /dev/null +++ b/src/infra/database/prisma/mappers/prisma-attachment-mapper.ts @@ -0,0 +1,33 @@ +import UniqueEntityId from '@/core/entities/unique-entity-id' +import { Attachment } from '@/domain/core/deliveries-and-orders/enterprise/entities/attachment' +import { PrismaAttachments } from '@prisma/client' + +export class PrismaAttachmentMapper { + static toDomain(prismaAttachment: PrismaAttachments): Attachment { + const orderId = prismaAttachment.orderId + ? new UniqueEntityId(prismaAttachment.orderId) + : null + + const update = Attachment.create( + { + title: prismaAttachment.title, + url: prismaAttachment.url, + orderId, + }, + new UniqueEntityId(prismaAttachment.id), + ) + + return update + } + + static domainToPrisma(attachment: Attachment): PrismaAttachments { + const prismaAttachment: PrismaAttachments = { + id: attachment.id.value, + orderId: attachment.id.value, + title: attachment.title, + url: attachment.url, + } + + return prismaAttachment + } +} diff --git a/src/infra/database/prisma/mappers/prisma-courier-mapper.ts b/src/infra/database/prisma/mappers/prisma-courier-mapper.ts new file mode 100644 index 0000000..bd24ead --- /dev/null +++ b/src/infra/database/prisma/mappers/prisma-courier-mapper.ts @@ -0,0 +1,58 @@ +import UniqueEntityId from '@/core/entities/unique-entity-id' +import { EntityTypeError } from '@/core/errors/errors/entity-type-error' +import { Courier } from '@/domain/core/deliveries-and-orders/enterprise/entities/courier' +import { + Coordinates, + CoordinatesProps, +} from '@/domain/core/deliveries-and-orders/enterprise/entities/value-objects/coordinates' +import { Cpf } from '@/domain/core/deliveries-and-orders/enterprise/entities/value-objects/cpf' +import { PrismaUser } from '@prisma/client' + +export class PrismaCourierMapper { + static toDomain(prismaCourier: PrismaUser): Courier { + if (prismaCourier.role !== 'courier') + throw new EntityTypeError( + `Courier.role must be equal "courier", but "${prismaCourier.role}" was passed`, + ) + + const coordinates = prismaCourier.coordinates + ? new Coordinates( + JSON.parse(prismaCourier.coordinates as string) as CoordinatesProps, + ) + : null + + const update = Courier.create( + { + cpf: new Cpf(prismaCourier.cpf), + name: prismaCourier.name, + password: prismaCourier.password, + role: prismaCourier.role, + coordinates, + createdAt: prismaCourier.createdAt, + updatedAt: prismaCourier.updatedAt, + }, + new UniqueEntityId(prismaCourier.id), + ) + + return update + } + + static domainToPrisma(courier: Courier): PrismaUser { + const coordinates = courier.coordinates?.raw + ? JSON.stringify(courier.coordinates.raw) + : null + + const prismaCourier: PrismaUser = { + id: courier.id.value, + cpf: courier.cpf.raw, + name: courier.name, + password: courier.password, + role: courier.role, + coordinates, + createdAt: courier.createdAt, + updatedAt: courier.updatedAt, + } + + return prismaCourier + } +} diff --git a/src/infra/database/prisma/mappers/prisma-order-attachment-mapper.ts b/src/infra/database/prisma/mappers/prisma-order-attachment-mapper.ts new file mode 100644 index 0000000..13ec520 --- /dev/null +++ b/src/infra/database/prisma/mappers/prisma-order-attachment-mapper.ts @@ -0,0 +1,33 @@ +import UniqueEntityId from '@/core/entities/unique-entity-id' +import { EntityTypeError } from '@/core/errors/errors/entity-type-error' +import { OrderAttachment } from '@/domain/core/deliveries-and-orders/enterprise/entities/order-attachment' +import { PrismaAttachments } from '@prisma/client' + +export class PrismaOrderAttachmentMapper { + static toDomain(prismaOrderAttachment: PrismaAttachments): OrderAttachment { + if (!prismaOrderAttachment.orderId) + throw new EntityTypeError( + `OrderAttachment.orderId must be a string, but a nullish value was passed`, + ) + + const update = OrderAttachment.create( + { + attachmentId: new UniqueEntityId(prismaOrderAttachment.id), + orderId: new UniqueEntityId(prismaOrderAttachment.orderId), + }, + new UniqueEntityId(prismaOrderAttachment.id), + ) + + return update + } + + // static domainToPrisma(OrderAttachment: OrderAttachment): PrismaAttachments { + // const prismaOrderAttachment: PrismaAttachments = { + // id: OrderAttachment.attachmentId.value, + // orderId: OrderAttachment.orderId.value, + + // } + + // return prismaOrderAttachment + // } +} diff --git a/src/infra/database/prisma/mappers/prisma-order-mapper.ts b/src/infra/database/prisma/mappers/prisma-order-mapper.ts new file mode 100644 index 0000000..fed1eaa --- /dev/null +++ b/src/infra/database/prisma/mappers/prisma-order-mapper.ts @@ -0,0 +1,79 @@ +import UniqueEntityId from '@/core/entities/unique-entity-id' +import { EntityTypeError } from '@/core/errors/errors/entity-type-error' +import { Attachment } from '@/domain/core/deliveries-and-orders/enterprise/entities/attachment' +import { Order } from '@/domain/core/deliveries-and-orders/enterprise/entities/order' +import { OrderAttachment } from '@/domain/core/deliveries-and-orders/enterprise/entities/order-attachment' +import { Address } from '@/domain/core/deliveries-and-orders/enterprise/entities/value-objects/address' +import { PrismaOrder } from '@prisma/client' + +export class PrismaOrderMapper { + static toDomain(prismaOrder: PrismaOrder): Order { + const address = new Address(JSON.parse(prismaOrder.address)) + const courierId = prismaOrder.courierId + ? new UniqueEntityId(prismaOrder.courierId) + : null + + const attachData = prismaOrder.deliveredPhoto + ? JSON.parse(prismaOrder.deliveredPhoto) + : null + + const attachment = attachData + ? Attachment.create(attachData, new UniqueEntityId(attachData.id)) + : null + + if (attachment && !attachment.orderId) + throw new EntityTypeError( + `when "PrismaOrder.deliveredPhoto" is not null, "PrismaOrder.deliveredPhoto.orderId" is obrigatory`, + ) + + const deliveredPhoto = attachment + ? OrderAttachment.create({ + attachmentId: attachment.id, + orderId: attachment.orderId!, + }) + : null + + const update = Order.create( + { + address, + courierId, + recipientId: new UniqueEntityId(prismaOrder.recipientId), + awaitingPickup: prismaOrder.awaitingPickup, + collected: prismaOrder.collected, + delivered: prismaOrder.delivered, + deliveredPhoto, + returned: prismaOrder.returned, + returnCause: prismaOrder.returnCause, + createdAt: prismaOrder.createdAt, + updatedAt: prismaOrder.updatedAt, + }, + new UniqueEntityId(prismaOrder.id), + ) + + return update + } + + static domainToPrisma(order: Order): PrismaOrder { + const address = JSON.stringify(order.address.raw) + const deliveredPhoto = order.deliveredPhoto?.raw + ? JSON.stringify(order.deliveredPhoto.raw) + : null + + const prismaOrder: PrismaOrder = { + id: order.id.value, + address, + courierId: order.courierId?.value ?? null, + recipientId: order.recipientId.value, + awaitingPickup: order.awaitingPickup, + collected: order.collected, + delivered: order.delivered, + deliveredPhoto, + returned: order.returned, + returnCause: order.returnCause, + createdAt: order.createdAt, + updatedAt: order.updatedAt, + } + + return prismaOrder + } +} diff --git a/src/infra/database/prisma/mappers/prisma-recipient-mapper.ts b/src/infra/database/prisma/mappers/prisma-recipient-mapper.ts new file mode 100644 index 0000000..564ba7b --- /dev/null +++ b/src/infra/database/prisma/mappers/prisma-recipient-mapper.ts @@ -0,0 +1,58 @@ +import UniqueEntityId from '@/core/entities/unique-entity-id' +import { EntityTypeError } from '@/core/errors/errors/entity-type-error' +import { Recipient } from '@/domain/core/deliveries-and-orders/enterprise/entities/recipient' +import { + Coordinates, + CoordinatesProps, +} from '@/domain/core/deliveries-and-orders/enterprise/entities/value-objects/coordinates' +import { Cpf } from '@/domain/core/deliveries-and-orders/enterprise/entities/value-objects/cpf' +import { PrismaUser } from '@prisma/client' + +export class PrismaRecipientMapper { + static toDomain(prismaRecipient: PrismaUser): Recipient { + if (prismaRecipient.role !== 'recipient') + throw new EntityTypeError( + `Recipient.role must be equal "recipient", but "${prismaRecipient.role}" was passed`, + ) + + const coordinates = prismaRecipient.coordinates + ? new Coordinates( + JSON.parse(prismaRecipient.coordinates as string) as CoordinatesProps, + ) + : null + + const update = Recipient.create( + { + cpf: new Cpf(prismaRecipient.cpf), + name: prismaRecipient.name, + password: prismaRecipient.password, + role: prismaRecipient.role, + coordinates, + createdAt: prismaRecipient.createdAt, + updatedAt: prismaRecipient.updatedAt, + }, + new UniqueEntityId(prismaRecipient.id), + ) + + return update + } + + static domainToPrisma(recipient: Recipient): PrismaUser { + const coordinates = recipient.coordinates?.raw + ? JSON.stringify(recipient.coordinates.raw) + : null + + const prismaRecipient: PrismaUser = { + id: recipient.id.value, + cpf: recipient.cpf.raw, + name: recipient.name, + password: recipient.password, + role: recipient.role, + coordinates, + createdAt: recipient.createdAt, + updatedAt: recipient.updatedAt, + } + + return prismaRecipient + } +} diff --git a/src/infra/database/prisma/mappers/prisma-update-adm-mapper.ts b/src/infra/database/prisma/mappers/prisma-update-adm-mapper.ts new file mode 100644 index 0000000..401d14e --- /dev/null +++ b/src/infra/database/prisma/mappers/prisma-update-adm-mapper.ts @@ -0,0 +1,54 @@ +import UniqueEntityId from '@/core/entities/unique-entity-id' +import { EntityTypeError } from '@/core/errors/errors/entity-type-error' +import { Adm } from '@/domain/core/deliveries-and-orders/enterprise/entities/adm' +import { UpdateAdm } from '@/domain/core/deliveries-and-orders/enterprise/entities/update-adm' +import { PrismaUpdates } from '@prisma/client' + +export class PrismaUpdateAdmMapper { + static toDomain(prismaUpdate: PrismaUpdates): UpdateAdm { + if (prismaUpdate.objectType !== 'adm') + throw new EntityTypeError( + `"UpdateAdm.objectType" needs to be "adm", but "${prismaUpdate.objectType}" was received`, + ) + + const changes: UpdateAdm['changes'] = prismaUpdate.changes + ? JSON.parse(prismaUpdate.changes as string) + : null + + const afterIsAdmObject = changes.after instanceof Adm + const beforeIsAdmObject = changes.before instanceof Adm + + if (!afterIsAdmObject || !beforeIsAdmObject) + throw new EntityTypeError( + `"UpdateAdm.changes.after" and "UpdateAdm.changes.before" needs to be an instance of "Adm".`, + ) + + const update = UpdateAdm.create( + { + changes, + objectId: new UniqueEntityId(prismaUpdate.objectId), + updatedBy: new UniqueEntityId(prismaUpdate.updatedBy), + date: new Date(prismaUpdate.date), + objectType: prismaUpdate.objectType, + }, + new UniqueEntityId(prismaUpdate.id), + ) + + return update + } + + static domainToPrisma(update: UpdateAdm): PrismaUpdates { + const changes = JSON.stringify(update.changes) + + const prismaUpdate: PrismaUpdates = { + changes, + date: update.date, + id: update.id.value, + objectId: update.objectId.value, + objectType: update.objectType, + updatedBy: update.updatedBy.value, + } + + return prismaUpdate + } +} diff --git a/src/infra/database/prisma/mappers/prisma-update-courier-mapper.ts b/src/infra/database/prisma/mappers/prisma-update-courier-mapper.ts new file mode 100644 index 0000000..5078fd1 --- /dev/null +++ b/src/infra/database/prisma/mappers/prisma-update-courier-mapper.ts @@ -0,0 +1,54 @@ +import UniqueEntityId from '@/core/entities/unique-entity-id' +import { EntityTypeError } from '@/core/errors/errors/entity-type-error' +import { Courier } from '@/domain/core/deliveries-and-orders/enterprise/entities/courier' +import { UpdateCourier } from '@/domain/core/deliveries-and-orders/enterprise/entities/update-courier' +import { PrismaUpdates } from '@prisma/client' + +export class PrismaUpdateCourierMapper { + static toDomain(prismaUpdate: PrismaUpdates): UpdateCourier { + if (prismaUpdate.objectType !== 'courier') + throw new EntityTypeError( + `"UpdateCourier.objectType" needs to be "courier", but "${prismaUpdate.objectType}" was received`, + ) + + const changes: UpdateCourier['changes'] = prismaUpdate.changes + ? JSON.parse(prismaUpdate.changes as string) + : null + + const afterIsCourierObject = changes.after instanceof Courier + const beforeIsCourierObject = changes.before instanceof Courier + + if (!afterIsCourierObject || !beforeIsCourierObject) + throw new EntityTypeError( + `"UpdateCourier.changes.after" and "UpdateCourier.changes.before" needs to be an instance of "Courier".`, + ) + + const update = UpdateCourier.create( + { + changes, + objectId: new UniqueEntityId(prismaUpdate.objectId), + updatedBy: new UniqueEntityId(prismaUpdate.updatedBy), + date: new Date(prismaUpdate.date), + objectType: prismaUpdate.objectType, + }, + new UniqueEntityId(prismaUpdate.id), + ) + + return update + } + + static domainToPrisma(update: UpdateCourier): PrismaUpdates { + const changes = JSON.stringify(update.changes) + + const prismaUpdate: PrismaUpdates = { + changes, + date: update.date, + id: update.id.value, + objectId: update.objectId.value, + objectType: update.objectType, + updatedBy: update.updatedBy.value, + } + + return prismaUpdate + } +} diff --git a/src/infra/database/prisma/mappers/prisma-update-order-mapper.ts b/src/infra/database/prisma/mappers/prisma-update-order-mapper.ts new file mode 100644 index 0000000..a7f76ec --- /dev/null +++ b/src/infra/database/prisma/mappers/prisma-update-order-mapper.ts @@ -0,0 +1,54 @@ +import UniqueEntityId from '@/core/entities/unique-entity-id' +import { EntityTypeError } from '@/core/errors/errors/entity-type-error' +import { Order } from '@/domain/core/deliveries-and-orders/enterprise/entities/order' +import { UpdateOrder } from '@/domain/core/deliveries-and-orders/enterprise/entities/update-order' +import { PrismaUpdates } from '@prisma/client' + +export class PrismaUpdateOrderMapper { + static toDomain(prismaUpdate: PrismaUpdates): UpdateOrder { + if (prismaUpdate.objectType !== 'order') + throw new EntityTypeError( + `"UpdateOrder.objectType" needs to be "order", but "${prismaUpdate.objectType}" was received`, + ) + + const changes: UpdateOrder['changes'] = prismaUpdate.changes + ? JSON.parse(prismaUpdate.changes as string) + : null + + const afterIsOrderObject = changes.after instanceof Order + const beforeIsOrderObject = changes.before instanceof Order + + if (!afterIsOrderObject || !beforeIsOrderObject) + throw new EntityTypeError( + `"UpdateOrder.changes.after" and "UpdateOrder.changes.before" needs to be an instance of "Order".`, + ) + + const update = UpdateOrder.create( + { + changes, + objectId: new UniqueEntityId(prismaUpdate.objectId), + updatedBy: new UniqueEntityId(prismaUpdate.updatedBy), + date: new Date(prismaUpdate.date), + objectType: prismaUpdate.objectType, + }, + new UniqueEntityId(prismaUpdate.id), + ) + + return update + } + + static domainToPrisma(update: UpdateOrder): PrismaUpdates { + const changes = JSON.stringify(update.changes) + + const prismaUpdate: PrismaUpdates = { + changes, + date: update.date, + id: update.id.value, + objectId: update.objectId.value, + objectType: update.objectType, + updatedBy: update.updatedBy.value, + } + + return prismaUpdate + } +} diff --git a/src/infra/database/prisma/mappers/prisma-update-recipient-mapper.ts b/src/infra/database/prisma/mappers/prisma-update-recipient-mapper.ts new file mode 100644 index 0000000..080a94f --- /dev/null +++ b/src/infra/database/prisma/mappers/prisma-update-recipient-mapper.ts @@ -0,0 +1,54 @@ +import UniqueEntityId from '@/core/entities/unique-entity-id' +import { EntityTypeError } from '@/core/errors/errors/entity-type-error' +import { Recipient } from '@/domain/core/deliveries-and-orders/enterprise/entities/recipient' +import { UpdateRecipient } from '@/domain/core/deliveries-and-orders/enterprise/entities/update-recipient' +import { PrismaUpdates } from '@prisma/client' + +export class PrismaUpdateRecipientMapper { + static toDomain(prismaUpdate: PrismaUpdates): UpdateRecipient { + if (prismaUpdate.objectType !== 'recipient') + throw new EntityTypeError( + `"UpdateRecipient.objectType" needs to be "recipient", but "${prismaUpdate.objectType}" was received`, + ) + + const changes: UpdateRecipient['changes'] = prismaUpdate.changes + ? JSON.parse(prismaUpdate.changes as string) + : null + + const afterIsRecipientObject = changes.after instanceof Recipient + const beforeIsRecipientObject = changes.before instanceof Recipient + + if (!afterIsRecipientObject || !beforeIsRecipientObject) + throw new EntityTypeError( + `"UpdateRecipient.changes.after" and "UpdateRecipient.changes.before" needs to be an instance of "Recipient".`, + ) + + const update = UpdateRecipient.create( + { + changes, + objectId: new UniqueEntityId(prismaUpdate.objectId), + updatedBy: new UniqueEntityId(prismaUpdate.updatedBy), + date: new Date(prismaUpdate.date), + objectType: prismaUpdate.objectType, + }, + new UniqueEntityId(prismaUpdate.id), + ) + + return update + } + + static domainToPrisma(update: UpdateRecipient): PrismaUpdates { + const changes = JSON.stringify(update.changes) + + const prismaUpdate: PrismaUpdates = { + changes, + date: update.date, + id: update.id.value, + objectId: update.objectId.value, + objectType: update.objectType, + updatedBy: update.updatedBy.value, + } + + return prismaUpdate + } +} diff --git a/src/infra/database/prisma/prisma.exports.ts b/src/infra/database/prisma/prisma.exports.ts new file mode 100644 index 0000000..e26c78b --- /dev/null +++ b/src/infra/database/prisma/prisma.exports.ts @@ -0,0 +1,19 @@ +import { Provider } from '@nestjs/common' + +const admRepositories: Provider[] = [] + +const courierRepositories: Provider[] = [] + +const orderRepositories: Provider[] = [] + +const recipientRepositories: Provider[] = [] + +const attachmentRepositories: Provider[] = [] + +export const repositories: Provider[] = [ + ...admRepositories, + ...courierRepositories, + ...orderRepositories, + ...recipientRepositories, + ...attachmentRepositories, +] diff --git a/src/infra/database/prisma/prisma.service.ts b/src/infra/database/prisma/prisma.service.ts new file mode 100644 index 0000000..0e623e9 --- /dev/null +++ b/src/infra/database/prisma/prisma.service.ts @@ -0,0 +1,22 @@ +import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common' +import { PrismaClient } from '@prisma/client' + +@Injectable() +export class PrismaService + extends PrismaClient + implements OnModuleInit, OnModuleDestroy +{ + constructor() { + super({ + log: ['error'], + }) + } + + async onModuleInit() { + return await this.$connect() + } + + async onModuleDestroy() { + return await this.$disconnect() + } +} diff --git a/src/infra/database/prisma/repositories/prisma-adm-repositories/prisma-adm-updates-repository.ts b/src/infra/database/prisma/repositories/prisma-adm-repositories/prisma-adm-updates-repository.ts new file mode 100644 index 0000000..b56e156 --- /dev/null +++ b/src/infra/database/prisma/repositories/prisma-adm-repositories/prisma-adm-updates-repository.ts @@ -0,0 +1,52 @@ +import { UpdateAdm } from '@/domain/core/deliveries-and-orders/enterprise/entities/update-adm' +import { PrismaService } from '../../prisma.service' +import { Injectable } from '@nestjs/common' +import { PrismaUpdateAdmMapper } from '../../mappers/prisma-update-adm-mapper' + +@Injectable() +export class PrismaAdmUpdatesRepository { + constructor(private prisma: PrismaService) {} + + async create(update: UpdateAdm): Promise { + await this.prisma.prismaUpdates.create({ + data: PrismaUpdateAdmMapper.domainToPrisma(update), + }) + } + + async update(update: UpdateAdm): Promise { + await this.prisma.prismaUpdates.update({ + where: { + id: update.id.value, + objectType: 'adm', + }, + data: PrismaUpdateAdmMapper.domainToPrisma(update), + }) + } + + async findById(id: string): Promise { + const data = await this.prisma.prismaUpdates.findUnique({ + where: { + id, + objectType: 'adm', + }, + }) + + if (!data) return null + + const mappedData = PrismaUpdateAdmMapper.toDomain(data) + + return mappedData + } + + async findMany(): Promise { + const data = await this.prisma.prismaUpdates.findMany({ + where: { + objectType: 'adm', + }, + }) + + const mappedData = data.map(PrismaUpdateAdmMapper.toDomain) + + return mappedData + } +} diff --git a/src/infra/database/prisma/repositories/prisma-adm-repositories/prisma-adms-repository.ts b/src/infra/database/prisma/repositories/prisma-adm-repositories/prisma-adms-repository.ts new file mode 100644 index 0000000..276037b --- /dev/null +++ b/src/infra/database/prisma/repositories/prisma-adm-repositories/prisma-adms-repository.ts @@ -0,0 +1,76 @@ +import { Adm } from '@/domain/core/deliveries-and-orders/enterprise/entities/adm' +import { Injectable } from '@nestjs/common' +import { PrismaService } from '../../prisma.service' +import { PrismaAdmMapper } from '../../mappers/prisma-adm-mapper' + +@Injectable() +export class PrismaAdmsRepository { + constructor(private prisma: PrismaService) {} + + async create(adm: Adm): Promise { + await this.prisma.prismaUser.create({ + data: PrismaAdmMapper.domainToPrisma(adm), + }) + } + + async update(adm: Adm): Promise { + await this.prisma.prismaUser.update({ + where: { + id: adm.id.value, + role: 'adm', + }, + data: PrismaAdmMapper.domainToPrisma(adm), + }) + } + + async delete(id: string): Promise { + await this.prisma.prismaUser.delete({ + where: { + id, + role: 'adm', + }, + }) + } + + async findById(id: string): Promise { + const data = await this.prisma.prismaUser.findUnique({ + where: { + id, + role: 'adm', + }, + }) + + if (!data) return null + + const mappedData = PrismaAdmMapper.toDomain(data) + + return mappedData + } + + async findByCpf(cpf: string): Promise { + const data = await this.prisma.prismaUser.findUnique({ + where: { + cpf, + role: 'adm', + }, + }) + + if (!data) return null + + const mappedData = PrismaAdmMapper.toDomain(data) + + return mappedData + } + + async findMany(): Promise { + const data = await this.prisma.prismaUser.findMany({ + where: { + role: 'adm', + }, + }) + + const mappedData = data.map(PrismaAdmMapper.toDomain) + + return mappedData + } +} diff --git a/src/infra/database/prisma/repositories/prisma-attachments-repository.ts b/src/infra/database/prisma/repositories/prisma-attachments-repository.ts new file mode 100644 index 0000000..a1a81e1 --- /dev/null +++ b/src/infra/database/prisma/repositories/prisma-attachments-repository.ts @@ -0,0 +1,12 @@ +import { Attachment } from '@/domain/core/deliveries-and-orders/enterprise/entities/attachment' +import { Injectable } from '@nestjs/common' +import { PrismaService } from '../../prisma.service' + +@Injectable() +export class PrismaAttachmentsRepository { + constructor(private prisma: PrismaService) {} + + async create(update: Attachment): Promise + async findById(id: string): Promise + async updateById(attachment: Attachment): Promise +} diff --git a/src/infra/database/prisma/repositories/prisma-courier-repositories/prisma-courier-repository.ts b/src/infra/database/prisma/repositories/prisma-courier-repositories/prisma-courier-repository.ts new file mode 100644 index 0000000..09ce14e --- /dev/null +++ b/src/infra/database/prisma/repositories/prisma-courier-repositories/prisma-courier-repository.ts @@ -0,0 +1,76 @@ +import { Courier } from '@/domain/core/deliveries-and-orders/enterprise/entities/courier' +import { Injectable } from '@nestjs/common' +import { PrismaService } from '../../prisma.service' +import { PrismaCourierMapper } from '../../mappers/prisma-courier-mapper' + +@Injectable() +export class PrismaCouriersRepository { + constructor(private prisma: PrismaService) {} + + async create(courier: Courier): Promise { + await this.prisma.prismaUser.create({ + data: PrismaCourierMapper.domainToPrisma(courier), + }) + } + + async update(courier: Courier): Promise { + await this.prisma.prismaUser.update({ + where: { + id: courier.id.value, + role: 'courier', + }, + data: PrismaCourierMapper.domainToPrisma(courier), + }) + } + + async delete(id: string): Promise { + await this.prisma.prismaUser.delete({ + where: { + id, + role: 'courier', + }, + }) + } + + async findById(id: string): Promise { + const data = await this.prisma.prismaUser.findUnique({ + where: { + id, + role: 'courier', + }, + }) + + if (!data) return null + + const mappedData = PrismaCourierMapper.toDomain(data) + + return mappedData + } + + async findByCpf(cpf: string): Promise { + const data = await this.prisma.prismaUser.findUnique({ + where: { + cpf, + role: 'courier', + }, + }) + + if (!data) return null + + const mappedData = PrismaCourierMapper.toDomain(data) + + return mappedData + } + + async findMany(): Promise { + const data = await this.prisma.prismaUser.findMany({ + where: { + role: 'courier', + }, + }) + + const mappedData = data.map(PrismaCourierMapper.toDomain) + + return mappedData + } +} diff --git a/src/infra/database/prisma/repositories/prisma-courier-repositories/prisma-courier-updates-repository.ts b/src/infra/database/prisma/repositories/prisma-courier-repositories/prisma-courier-updates-repository.ts new file mode 100644 index 0000000..38d5523 --- /dev/null +++ b/src/infra/database/prisma/repositories/prisma-courier-repositories/prisma-courier-updates-repository.ts @@ -0,0 +1,52 @@ +import { UpdateCourier } from '@/domain/core/deliveries-and-orders/enterprise/entities/update-courier' +import { PrismaService } from '../../prisma.service' +import { Injectable } from '@nestjs/common' +import { PrismaUpdateCourierMapper } from '../../mappers/prisma-update-courier-mapper' + +@Injectable() +export class PrismaCourierUpdatesRepository { + constructor(private prisma: PrismaService) {} + + async create(update: UpdateCourier): Promise { + await this.prisma.prismaUpdates.create({ + data: PrismaUpdateCourierMapper.domainToPrisma(update), + }) + } + + async update(update: UpdateCourier): Promise { + await this.prisma.prismaUpdates.update({ + where: { + id: update.id.value, + objectType: 'courier', + }, + data: PrismaUpdateCourierMapper.domainToPrisma(update), + }) + } + + async findById(id: string): Promise { + const data = await this.prisma.prismaUpdates.findUnique({ + where: { + id, + objectType: 'courier', + }, + }) + + if (!data) return null + + const mappedData = PrismaUpdateCourierMapper.toDomain(data) + + return mappedData + } + + async findMany(): Promise { + const data = await this.prisma.prismaUpdates.findMany({ + where: { + objectType: 'courier', + }, + }) + + const mappedData = data.map(PrismaUpdateCourierMapper.toDomain) + + return mappedData + } +} diff --git a/src/infra/database/prisma/repositories/prisma-order-repositories/prisma-order-attachments-repository.ts b/src/infra/database/prisma/repositories/prisma-order-repositories/prisma-order-attachments-repository.ts new file mode 100644 index 0000000..e37f98b --- /dev/null +++ b/src/infra/database/prisma/repositories/prisma-order-repositories/prisma-order-attachments-repository.ts @@ -0,0 +1,15 @@ +import { OrderAttachment } from '@/domain/core/deliveries-and-orders/enterprise/entities/order-attachment' +import { Injectable } from '@nestjs/common' +import { PrismaService } from '../../prisma.service' + +@Injectable() +export class PrismaOrderAttachmentsRepository { + constructor(private prisma: PrismaService) {} + + async create(update: OrderAttachment): Promise + async findByOrderId(orderId: string): Promise + async findById(id: string): Promise + async updateById(orderAttachment: OrderAttachment): Promise + + async deleteByOrderId(orderId: string): Promise +} diff --git a/src/infra/database/prisma/repositories/prisma-order-repositories/prisma-order-updates-repository.ts b/src/infra/database/prisma/repositories/prisma-order-repositories/prisma-order-updates-repository.ts new file mode 100644 index 0000000..118fb5e --- /dev/null +++ b/src/infra/database/prisma/repositories/prisma-order-repositories/prisma-order-updates-repository.ts @@ -0,0 +1,52 @@ +import { UpdateOrder } from '@/domain/core/deliveries-and-orders/enterprise/entities/update-order' +import { PrismaService } from '../../prisma.service' +import { Injectable } from '@nestjs/common' +import { PrismaUpdateOrderMapper } from '../../mappers/prisma-update-order-mapper' + +@Injectable() +export class PrismaOrderUpdatesRepository { + constructor(private prisma: PrismaService) {} + + async create(update: UpdateOrder): Promise { + await this.prisma.prismaUpdates.create({ + data: PrismaUpdateOrderMapper.domainToPrisma(update), + }) + } + + async update(update: UpdateOrder): Promise { + await this.prisma.prismaUpdates.update({ + where: { + id: update.id.value, + objectType: 'order', + }, + data: PrismaUpdateOrderMapper.domainToPrisma(update), + }) + } + + async findById(id: string): Promise { + const data = await this.prisma.prismaUpdates.findUnique({ + where: { + id, + objectType: 'order', + }, + }) + + if (!data) return null + + const mappedData = PrismaUpdateOrderMapper.toDomain(data) + + return mappedData + } + + async findMany(): Promise { + const data = await this.prisma.prismaUpdates.findMany({ + where: { + objectType: 'order', + }, + }) + + const mappedData = data.map(PrismaUpdateOrderMapper.toDomain) + + return mappedData + } +} diff --git a/src/infra/database/prisma/repositories/prisma-order-repositories/prisma-orders-repository.ts b/src/infra/database/prisma/repositories/prisma-order-repositories/prisma-orders-repository.ts new file mode 100644 index 0000000..e8e4582 --- /dev/null +++ b/src/infra/database/prisma/repositories/prisma-order-repositories/prisma-orders-repository.ts @@ -0,0 +1,52 @@ +import { Order } from '@/domain/core/deliveries-and-orders/enterprise/entities/order' +import { Coordinates } from '@/domain/core/deliveries-and-orders/enterprise/entities/value-objects/coordinates' +import { Injectable } from '@nestjs/common' +import { PrismaService } from '../../prisma.service' +import { PrismaOrderMapper } from '../../mappers/prisma-order-mapper' + +@Injectable() +export class PrismaOrdersRepository { + constructor(private prisma: PrismaService) {} + + async create(order: Order): Promise { + await this.prisma.prismaUser.create({ + data: PrismaOrderMapper.domainToPrisma(order), + }) + } + + async update(order: Order): Promise { + await this.prisma.prismaUser.update({ + where: { + id: order.id.value, + }, + data: PrismaOrderMapper.domainToPrisma(order), + }) + } + + async delete(id: string): Promise { + await this.prisma.prismaUser.delete({ + where: { + id, + }, + }) + } + + async findById(id: string): Promise { + const data = await this.prisma.prismaUser.findUnique({ + where: { + id, + }, + }) + + if (!data) return null + + const mappedData = PrismaOrderMapper.toDomain(data) + + return mappedData + } + + async findMany(): Promise + async findManyNearBy(coordinates: Coordinates): Promise + async findManyByOrderId(orderId: string): Promise + async findManyByRecipientId(recipientId: string): Promise +} diff --git a/src/infra/database/prisma/repositories/prisma-recipient-repositories/prisma-recipient-updates-repository.ts b/src/infra/database/prisma/repositories/prisma-recipient-repositories/prisma-recipient-updates-repository.ts new file mode 100644 index 0000000..baa3625 --- /dev/null +++ b/src/infra/database/prisma/repositories/prisma-recipient-repositories/prisma-recipient-updates-repository.ts @@ -0,0 +1,52 @@ +import { UpdateRecipient } from '@/domain/core/deliveries-and-orders/enterprise/entities/update-recipient' +import { PrismaService } from '../../prisma.service' +import { Injectable } from '@nestjs/common' +import { PrismaUpdateRecipientMapper } from '../../mappers/prisma-update-recipient-mapper' + +@Injectable() +export class PrismaRecipientUpdatesRepository { + constructor(private prisma: PrismaService) {} + + async create(update: UpdateRecipient): Promise { + await this.prisma.prismaUpdates.create({ + data: PrismaUpdateRecipientMapper.domainToPrisma(update), + }) + } + + async update(update: UpdateRecipient): Promise { + await this.prisma.prismaUpdates.update({ + where: { + id: update.id.value, + objectType: 'recipient', + }, + data: PrismaUpdateRecipientMapper.domainToPrisma(update), + }) + } + + async findById(id: string): Promise { + const data = await this.prisma.prismaUpdates.findUnique({ + where: { + id, + objectType: 'recipient', + }, + }) + + if (!data) return null + + const mappedData = PrismaUpdateRecipientMapper.toDomain(data) + + return mappedData + } + + async findMany(): Promise { + const data = await this.prisma.prismaUpdates.findMany({ + where: { + objectType: 'recipient', + }, + }) + + const mappedData = data.map(PrismaUpdateRecipientMapper.toDomain) + + return mappedData + } +} diff --git a/src/infra/database/prisma/repositories/prisma-recipient-repositories/prisma-recipients-repository.ts b/src/infra/database/prisma/repositories/prisma-recipient-repositories/prisma-recipients-repository.ts new file mode 100644 index 0000000..fc455e9 --- /dev/null +++ b/src/infra/database/prisma/repositories/prisma-recipient-repositories/prisma-recipients-repository.ts @@ -0,0 +1,76 @@ +import { Recipient } from '@/domain/core/deliveries-and-orders/enterprise/entities/recipient' +import { Injectable } from '@nestjs/common' +import { PrismaService } from '../../prisma.service' +import { PrismaRecipientMapper } from '../../mappers/prisma-recipient-mapper' + +@Injectable() +export class PrismaRecipientsRepository { + constructor(private prisma: PrismaService) {} + + async create(recipient: Recipient): Promise { + await this.prisma.prismaUser.create({ + data: PrismaRecipientMapper.domainToPrisma(recipient), + }) + } + + async update(recipient: Recipient): Promise { + await this.prisma.prismaUser.update({ + where: { + id: recipient.id.value, + role: 'recipient', + }, + data: PrismaRecipientMapper.domainToPrisma(recipient), + }) + } + + async delete(id: string): Promise { + await this.prisma.prismaUser.delete({ + where: { + id, + role: 'recipient', + }, + }) + } + + async findById(id: string): Promise { + const data = await this.prisma.prismaUser.findUnique({ + where: { + id, + role: 'recipient', + }, + }) + + if (!data) return null + + const mappedData = PrismaRecipientMapper.toDomain(data) + + return mappedData + } + + async findByCpf(cpf: string): Promise { + const data = await this.prisma.prismaUser.findUnique({ + where: { + cpf, + role: 'recipient', + }, + }) + + if (!data) return null + + const mappedData = PrismaRecipientMapper.toDomain(data) + + return mappedData + } + + async findMany(): Promise { + const data = await this.prisma.prismaUser.findMany({ + where: { + role: 'recipient', + }, + }) + + const mappedData = data.map(PrismaRecipientMapper.toDomain) + + return mappedData + } +}