Skip to content
This repository was archived by the owner on Jul 14, 2024. It is now read-only.

Commit

Permalink
feat: add maintenance company in graphql and application layer
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielDeSouzza committed Apr 6, 2024
1 parent f7cfda4 commit 10469c9
Show file tree
Hide file tree
Showing 25 changed files with 700 additions and 63 deletions.
10 changes: 5 additions & 5 deletions prisma/dbml/schema.dbml
Original file line number Diff line number Diff line change
Expand Up @@ -668,10 +668,10 @@ Table types_of_maintenances {

Table maintenance_companies {
id String [pk]
specialty_maintenance String [not null]
specialty_maintenance String
Maintenance maintenance [not null]
LegalPerson legal_people [not null]
cnpj String [unique, not null]
legal_person_id String [unique, not null]
created_at DateTime [default: `now()`, not null]
updated_at DateTime [default: `now()`, not null]
CreatedBy users [not null]
Expand All @@ -683,7 +683,7 @@ Table maintenance_companies {
Table maintenance {
id String [pk]
MaintenanceCompany maintenance_companies [not null]
maintenance_company_cnpj String [not null]
maintenance_company_id String [not null]
Vehicle vehicles [not null]
plate String [not null]
TypeOfMaintenance types_of_maintenances [not null]
Expand Down Expand Up @@ -898,13 +898,13 @@ Ref: types_of_maintenances.created_by > users.id

Ref: types_of_maintenances.updated_by > users.id

Ref: maintenance_companies.cnpj - legal_people.cnpj
Ref: maintenance_companies.legal_person_id - legal_people.id

Ref: maintenance_companies.created_by > users.id

Ref: maintenance_companies.updated_by > users.id

Ref: maintenance.maintenance_company_cnpj > maintenance_companies.cnpj
Ref: maintenance.maintenance_company_id > maintenance_companies.id

Ref: maintenance.plate > vehicles.plate

Expand Down
36 changes: 36 additions & 0 deletions prisma/migrations/20240406132931_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Warnings:
- You are about to drop the column `maintenance_company_cnpj` on the `maintenance` table. All the data in the column will be lost.
- You are about to drop the column `cnpj` on the `maintenance_companies` table. All the data in the column will be lost.
- A unique constraint covering the columns `[legal_person_id]` on the table `maintenance_companies` will be added. If there are existing duplicate values, this will fail.
- Added the required column `maintenance_company_id` to the `maintenance` table without a default value. This is not possible if the table is not empty.
- Added the required column `legal_person_id` to the `maintenance_companies` table without a default value. This is not possible if the table is not empty.
*/
-- DropForeignKey
ALTER TABLE "maintenance" DROP CONSTRAINT "maintenance_maintenance_company_cnpj_fkey";

-- DropForeignKey
ALTER TABLE "maintenance_companies" DROP CONSTRAINT "maintenance_companies_cnpj_fkey";

-- DropIndex
DROP INDEX "maintenance_companies_cnpj_key";

-- AlterTable
ALTER TABLE "maintenance" DROP COLUMN "maintenance_company_cnpj",
ADD COLUMN "maintenance_company_id" TEXT NOT NULL;

-- AlterTable
ALTER TABLE "maintenance_companies" DROP COLUMN "cnpj",
ADD COLUMN "legal_person_id" TEXT NOT NULL,
ALTER COLUMN "specialty_maintenance" DROP NOT NULL;

-- CreateIndex
CREATE UNIQUE INDEX "maintenance_companies_legal_person_id_key" ON "maintenance_companies"("legal_person_id");

-- AddForeignKey
ALTER TABLE "maintenance_companies" ADD CONSTRAINT "maintenance_companies_legal_person_id_fkey" FOREIGN KEY ("legal_person_id") REFERENCES "legal_people"("cnpj") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "maintenance" ADD CONSTRAINT "maintenance_maintenance_company_id_fkey" FOREIGN KEY ("maintenance_company_id") REFERENCES "maintenance_companies"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
5 changes: 5 additions & 0 deletions prisma/migrations/20240406135042_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- DropForeignKey
ALTER TABLE "maintenance_companies" DROP CONSTRAINT "maintenance_companies_legal_person_id_fkey";

-- AddForeignKey
ALTER TABLE "maintenance_companies" ADD CONSTRAINT "maintenance_companies_legal_person_id_fkey" FOREIGN KEY ("legal_person_id") REFERENCES "legal_people"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
44 changes: 22 additions & 22 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -768,38 +768,38 @@ model TypeOfMaintenance {
}

model MaintenanceCompany {
id String @id @default(uuid())
specialty_maintenance String
id String @id @default(uuid())
specialty_maintenance String?
Maintenance Maintenance[]
LegalPerson LegalPerson @relation(fields: [cnpj], references: [cnpj])
cnpj String @unique
created_at DateTime @default(now())
updated_at DateTime @default(now())
CreatedBy User @relation("created_by", fields: [created_by], references: [id])
created_by String
UpdatedBy User @relation("updated_by", fields: [updated_by], references: [id])
updated_by String
LegalPerson LegalPerson @relation(fields: [legal_person_id], references: [id])
legal_person_id String @unique
created_at DateTime @default(now())
updated_at DateTime @default(now())
CreatedBy User @relation("created_by", fields: [created_by], references: [id])
created_by String
UpdatedBy User @relation("updated_by", fields: [updated_by], references: [id])
updated_by String
@@map("maintenance_companies")
}

model Maintenance {
id String @id @default(uuid())
MaintenanceCompany MaintenanceCompany @relation(fields: [maintenance_company_cnpj], references: [cnpj])
maintenance_company_cnpj String
Vehicle Vehicle @relation(fields: [plate], references: [plate])
plate String
TypeOfMaintenance TypeOfMaintenance @relation(fields: [maintenance_process_id], references: [id])
maintenance_process_id String
created_at DateTime @default(now())
updated_at DateTime @default(now())
CreatedBy User @relation("created_by", fields: [created_by], references: [id])
created_by String
UpdatedBy User @relation("updated_by", fields: [updated_by], references: [id])
updated_by String
MaintenanceCompany MaintenanceCompany @relation(fields: [maintenance_company_id], references: [id])
maintenance_company_id String
Vehicle Vehicle @relation(fields: [plate], references: [plate])
plate String
TypeOfMaintenance TypeOfMaintenance @relation(fields: [maintenance_process_id], references: [id])
maintenance_process_id String
created_at DateTime @default(now())
updated_at DateTime @default(now())
CreatedBy User @relation("created_by", fields: [created_by], references: [id])
created_by String
UpdatedBy User @relation("updated_by", fields: [updated_by], references: [id])
updated_by String
@@map("maintenance")
}
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { LegalClientModule } from 'infra/graphql/entities/LegalClientGraphql/Leg
import { LegalClientOrderModule } from 'infra/graphql/entities/LegalClientOrderGraphql/LegalClientOrder.module';
import { LegalClientQuoteTableModule } from 'infra/graphql/entities/LegalClientQuoteTableGraphql/LegalClientQuoteTable.module';
import { LegalContractModule } from 'infra/graphql/entities/LegalContractGraphql/LegalContract.module';
import { MaintenanceCompanyModule } from 'infra/graphql/entities/MaintenanceCompanyGraphql/MaintenanceCompany.module';
import { OrderProcessingModule } from 'infra/graphql/entities/OrderProcessingGraphql/OrderProcessing.module';
import { OutsourcedDriverModule } from 'infra/graphql/entities/OutsourcedDriverGraphql/OutsourcedDriver.module';
import { OutsourcedTransportCompanyContractModule } from 'infra/graphql/entities/OutsourcedTransportCompanyContractGraphql/OutsourcedTransportCompanyContract.module';
Expand Down Expand Up @@ -94,6 +95,7 @@ import { AuthModule } from 'infra/guard/auth.module';
FreightExpenseModule,
CtePdfModule,
TypeOfMaintenanceModule,
MaintenanceCompanyModule,
],
})
export class AppModule {}
13 changes: 13 additions & 0 deletions src/app/dtos/MaintenanceCompanyDto/CreateMaintenanceCompanyDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { type CreateLegalPersonDTO } from '../LegalPerson/CreateLegalPersonDto';

export abstract class CreateMaintenanceCompanyDTO {
legal_person_id?: string;

specialty_maintenance: string;

LegalPerson?: CreateLegalPersonDTO;

updated_by: string;

created_by?: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { type UpdateLegalPersonDTO } from '../LegalPerson/UpdateLegalPersonDto';

export abstract class UpdateMaintenanceCompanyDTO {
specialty_maintenance?: string;

LegalPerson: UpdateLegalPersonDTO;

updated_by?: string;
}
4 changes: 2 additions & 2 deletions src/app/useCases/LegalPersonUseCases/LegalPersonUseCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class LegalPersonUseCases {
state_registration: data?.state_registration,
id: data?.id,
});
console.log(data);
console.log(person);
if (person) {
let errors = '';

Expand All @@ -63,7 +63,7 @@ export class LegalPersonUseCases {
extensions: { code: HttpStatus.CONFLICT },
});
}
} else if (!person?.id)
} else if (person?.id)
throw new GraphQLError('LEGAL PERSON NOT FOUND', {
extensions: { code: HttpStatus.NOT_FOUND },
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { HttpStatus, Injectable } from '@nestjs/common';

import { GraphQLError } from 'graphql';

import { type GetMaintenanceCompanyDTO } from 'domain/dto/repositories/getDataDtos/GetMaintenanceCompanyDto';
import { type FindAllMaintenanceCompanyWhereRequestDTO } from 'domain/dto/repositories/whereDtos/MaintenanceCompanyRepositoryDto';
import { MaintenanceCompany } from 'domain/entities/MaintenceEntities/MaintenanceCompany/MaintenanceCompany';
import { MaintenanceCompanyRepository } from 'domain/repositories/MaintenanceCompanyRepositoy';

import { LegalPersonEntityDto } from 'app/dtos/LegalPerson/LegalPersonEntityDto';
import { type CreateMaintenanceCompanyDTO } from 'app/dtos/MaintenanceCompanyDto/CreateMaintenanceCompanyDto';
import { type UpdateMaintenanceCompanyDTO } from 'app/dtos/MaintenanceCompanyDto/UpdateMaintenanceCompanyDto';

import { LegalPersonUseCases } from '../LegalPersonUseCases/LegalPersonUseCases';

@Injectable()
export class MaintenanceCompanyUseCases {
constructor(
private maintenanceCompanyRepository: MaintenanceCompanyRepository,
private legalPersonUseCase: LegalPersonUseCases,
) {}
async getMaintenanceCompany(request: GetMaintenanceCompanyDTO) {
if (
!request.cnpj &&
!request.corporateName &&
!request.fantasyName &&
!request.id &&
!request.legalPersonId
)
throw new GraphQLError(
'IS NECESSARY IN CNPJ, CORPORATENAME, FANTASYNAME, ID OR LEGALPERSONID',
{ extensions: { code: HttpStatus.BAD_REQUEST } },
);
const maintenancecompany =
await this.maintenanceCompanyRepository.findMaintenanceCompany(request);
if (maintenancecompany) return maintenancecompany;

throw new GraphQLError('MaintenanceCompany Not Found', {
extensions: { code: HttpStatus.NOT_FOUND },
});
}

async getAllMaintenanceCompanys(
request: FindAllMaintenanceCompanyWhereRequestDTO,
) {
const companies =
await this.maintenanceCompanyRepository.getAllMaintenanceCompany(request);
if (companies.length === 0)
throw new GraphQLError('ANY MAINTENANCE COMPANY FOUND', {
extensions: { code: HttpStatus.NOT_FOUND },
});

return companies;
}

async createMaintenanceCompany(data: CreateMaintenanceCompanyDTO) {
if (data.LegalPerson)
await this.legalPersonUseCase.validatePerson(data.LegalPerson);
const maintenanceCompany = new MaintenanceCompany({
specialty_maintenance: data.specialty_maintenance,
updated_by: data.updated_by,
created_by: data.created_by,
legal_person_id: data.legal_person_id,
});
const legalPerson = LegalPersonEntityDto.createEntity(data.LegalPerson);

return this.maintenanceCompanyRepository.createMaintenanceCompany(
maintenanceCompany,
legalPerson,
data.legal_person_id,
);
}

async updateMaintenanceCompany(
id: string,
data: UpdateMaintenanceCompanyDTO,
) {
const legalPerson = LegalPersonEntityDto.updateEntity(data.LegalPerson);
const maintenanceCompany = new MaintenanceCompany({
specialty_maintenance: data.specialty_maintenance,
updated_by: data.updated_by,
created_by: null,
legal_person_id: null,
});

return this.maintenanceCompanyRepository.updateMaintenanceCompany(
id,
maintenanceCompany,
legalPerson,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { GetLegalPersonDTO } from './GetLegalPersonDto';

export abstract class GetMaintenanceCompanyDTO extends GetLegalPersonDTO {
id?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ export abstract class WhereMaintenanceCompanyTypeDTO extends WhereDTO {
id?: StringFilterDTO;
specialty_maintenance?: StringFilterDTO;
legal_person_id?: StringFilterDTO;
created_at?: DateTimeFilterDTO;
updated_at?: DateTimeFilterDTO;
created_at?: DateTimeFilterDTO;
updated_by?: StringFilterDTO;
created_by?: StringFilterDTO;
}

export abstract class SortByMaintenanceCompanyTypeDTO {
id?: 'asc' | 'desc';
specialty_maintenance?: 'asc' | 'desc';
legal_person_id?: 'asc' | 'desc';
created_at?: 'asc' | 'desc';
updated_at?: 'asc' | 'desc';
created_at?: 'asc' | 'desc';
updated_by?: 'asc' | 'desc';
created_by?: 'asc' | 'desc';
}

export class FindAllMaintenanceCompanyWhereRequestDTO {
Expand Down
26 changes: 0 additions & 26 deletions src/domain/dto/repositories/whereDtos/MaintenceRepositoryDto.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ describe('MaintenanceCompany', () => {
updated_at: new Date(),
id: 'sdsd',
specialty_maintenance: 'Pneumatica',
created_by: '123',
updated_by: '1313',
});

expect(maintenanceCompany).toBeTruthy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { Entity } from '../../../shared/entities/Entity';
import { type IValidationField } from '../../../shared/notification/Notification';
import { NotificationError } from '../../../shared/notification/NotificationError';

interface IMaintenanceCompany {
export interface IMaintenanceCompany {
id?: string;
specialty_maintenance: string;
specialty_maintenance?: string;
legal_person_id?: string;
created_at: Date;
updated_at: Date;
created_by: string;
updated_by: string;
}

export class MaintenanceCompany extends Entity {
Expand Down Expand Up @@ -97,4 +99,20 @@ export class MaintenanceCompany extends Entity {
public get updated_at(): Date {
return this.props.updated_at;
}

get updated_by(): string {
return this.props.updated_by;
}

set updated_by(updated_by: string) {
this.props.updated_by = updated_by;
}

get created_by(): string {
return this.props.created_by;
}

set created_by(created_by: string) {
this.props.created_by = created_by;
}
}
Loading

0 comments on commit 10469c9

Please sign in to comment.