-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feat-hotwire
- Loading branch information
Showing
37 changed files
with
178 additions
and
448 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
||
export class DropAddress1699290758562 implements MigrationInterface { | ||
name = 'DropAddress1699290758562' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "customer" DROP CONSTRAINT "FK_7697a356e1f4b79ab3819839e95"`); | ||
await queryRunner.query(`ALTER TABLE "cooperative" DROP CONSTRAINT "FK_76076f95b4afaa794ca4a974661"`); | ||
await queryRunner.query(`ALTER TABLE "customer" DROP COLUMN "addressId"`); | ||
await queryRunner.query(`ALTER TABLE "cooperative" DROP COLUMN "addressId"`); | ||
await queryRunner.query(`DROP TABLE "address"`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`CREATE TABLE "address" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "street" character varying NOT NULL, "city" character varying NOT NULL, "zipCode" character varying(6) NOT NULL, "country" character varying(2) NOT NULL, CONSTRAINT "PK_d92de1f82754668b5f5f5dd4fd5" PRIMARY KEY ("id"))`); | ||
await queryRunner.query(`ALTER TABLE "cooperative" ADD "addressId" uuid NOT NULL`); | ||
await queryRunner.query(`ALTER TABLE "customer" ADD "addressId" uuid`); | ||
await queryRunner.query(`ALTER TABLE "cooperative" ADD CONSTRAINT "FK_76076f95b4afaa794ca4a974661" FOREIGN KEY ("addressId") REFERENCES "address"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); | ||
await queryRunner.query(`ALTER TABLE "customer" ADD CONSTRAINT "FK_7697a356e1f4b79ab3819839e95" FOREIGN KEY ("addressId") REFERENCES "address"("id") ON DELETE SET NULL ON UPDATE NO ACTION`); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
server/src/Infrastructure/Customer/Action/CreateCustomerAction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { | ||
Body, | ||
Post, | ||
Controller, | ||
Inject, | ||
BadRequestException, | ||
UseGuards | ||
} from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger'; | ||
import { CreateCustomerCommand } from 'src/Application/Customer/Command/CreateCustomerCommand'; | ||
import { ICommandBus } from 'src/Application/ICommandBus'; | ||
import { CustomerDTO } from '../DTO/CustomerDTO'; | ||
import { RolesGuard } from 'src/Infrastructure/HumanResource/User/Security/RolesGuard'; | ||
import { UserRole } from 'src/Domain/HumanResource/User/User.entity'; | ||
import { Roles } from 'src/Infrastructure/HumanResource/User/Decorator/Roles'; | ||
|
||
@Controller('customers') | ||
@ApiTags('Customer') | ||
@ApiBearerAuth() | ||
@UseGuards(AuthGuard('bearer'), RolesGuard) | ||
export class CreateCustomerAction { | ||
constructor( | ||
@Inject('ICommandBus') | ||
private readonly commandBus: ICommandBus | ||
) {} | ||
|
||
@Post() | ||
@Roles(UserRole.COOPERATOR, UserRole.EMPLOYEE) | ||
@ApiOperation({ summary: 'Create new customer' }) | ||
public async index(@Body() customerDto: CustomerDTO) { | ||
const { name } = customerDto; | ||
|
||
try { | ||
const id = await this.commandBus.execute(new CreateCustomerCommand(name)); | ||
|
||
return { id }; | ||
} catch (e) { | ||
throw new BadRequestException(e.message); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
server/src/Infrastructure/Customer/Action/UpdateCustomerAction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Inject, | ||
BadRequestException, | ||
UseGuards, | ||
Put, | ||
Param | ||
} from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger'; | ||
import { ICommandBus } from 'src/Application/ICommandBus'; | ||
import { UpdateCustomerCommand } from 'src/Application/Customer/Command/UpdateCustomerCommand'; | ||
import { CustomerDTO } from '../DTO/CustomerDTO'; | ||
import { IdDTO } from 'src/Infrastructure/Common/DTO/IdDTO'; | ||
import { RolesGuard } from 'src/Infrastructure/HumanResource/User/Security/RolesGuard'; | ||
import { Roles } from 'src/Infrastructure/HumanResource/User/Decorator/Roles'; | ||
import { UserRole } from 'src/Domain/HumanResource/User/User.entity'; | ||
|
||
@Controller('customers') | ||
@ApiTags('Customer') | ||
@ApiBearerAuth() | ||
@UseGuards(AuthGuard('bearer'), RolesGuard) | ||
export class UpdateCustomerAction { | ||
constructor( | ||
@Inject('ICommandBus') | ||
private readonly commandBus: ICommandBus | ||
) {} | ||
|
||
@Put(':id') | ||
@Roles(UserRole.COOPERATOR, UserRole.EMPLOYEE) | ||
@ApiOperation({ summary: 'Update customer' }) | ||
public async index(@Param() dto: IdDTO, @Body() customerDto: CustomerDTO) { | ||
try { | ||
const { name } = customerDto; | ||
const { id } = dto; | ||
|
||
await this.commandBus.execute(new UpdateCustomerCommand(id, name)); | ||
|
||
return { id }; | ||
} catch (e) { | ||
throw new BadRequestException(e.message); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
server/src/Infrastructure/Customer/DTO/CustomerDTO.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { CustomerDTO } from './CustomerDTO'; | ||
import { validate } from 'class-validator'; | ||
|
||
describe('CustomerDTO', () => { | ||
it('testValidDTO', async () => { | ||
const dto = new CustomerDTO(); | ||
dto.name = 'Customer'; | ||
|
||
const validation = await validate(dto); | ||
expect(validation).toHaveLength(0); | ||
}); | ||
|
||
it('testInvalidDTO', async () => { | ||
const dto = new CustomerDTO(); | ||
dto.name = ''; | ||
|
||
const validation = await validate(dto); | ||
expect(validation).toHaveLength(1); | ||
expect(validation[0].constraints).toMatchObject({ | ||
isNotEmpty: 'name should not be empty' | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsNotEmpty } from 'class-validator'; | ||
|
||
export class CustomerDTO { | ||
@ApiProperty() | ||
@IsNotEmpty() | ||
public name: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,5 @@ | ||
import { ICommand } from 'src/Application/ICommand'; | ||
|
||
export class CreateCustomerCommand implements ICommand { | ||
constructor( | ||
public readonly name: string, | ||
public readonly street: string, | ||
public readonly city: string, | ||
public readonly zipCode: string, | ||
public readonly country: string | ||
) {} | ||
constructor(public readonly name: string) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,5 @@ | ||
import { ICommand } from 'src/Application/ICommand'; | ||
|
||
export class UpdateCustomerCommand implements ICommand { | ||
constructor( | ||
public readonly id: string, | ||
public readonly name: string, | ||
public readonly street: string, | ||
public readonly city: string, | ||
public readonly zipCode: string, | ||
public readonly country: string | ||
) {} | ||
constructor(public readonly id: string, public readonly name: string) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.