Skip to content

Commit

Permalink
feat(backend): wip: ambientes/campus: campusDeleteOneById
Browse files Browse the repository at this point in the history
  • Loading branch information
guesant committed Feb 13, 2024
1 parent 651ae4c commit c248a7a
Show file tree
Hide file tree
Showing 14 changed files with 179 additions and 11 deletions.
16 changes: 15 additions & 1 deletion luna-backend/src/application/authz/AuthzPublicProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class AuthzPublicProvider extends BaseAuthzProvider {
this.campusFind,
this.campusCreate,
this.campusUpdate,
this.campusDelete,
];
}

Expand Down Expand Up @@ -47,7 +48,7 @@ export class AuthzPublicProvider extends BaseAuthzProvider {
compositeMode: 'include',

filter(qb) {
qb.where('TRUE');
qb.where('campus.dateDeleted IS NULL');
},
});
}
Expand Down Expand Up @@ -77,4 +78,17 @@ export class AuthzPublicProvider extends BaseAuthzProvider {
},
});
}

get campusDelete() {
return createStatement({
action: 'delete',

target: 'campus',

async check(context, requestUser) {
console.debug('delete', 'campus', { context, requestUser });
return true;
},
});
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ICampusFindOneByIdInputDto } from '../campus-find-one';

export interface ICampusDeleteOneByIdInputDto extends ICampusFindOneByIdInputDto {
id: string;
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './ICampusDeleteByIdInputDto';
export * from './ICampusDeleteOneByIdInputDto';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get, Post, Put } from '@nestjs/common';
import { Controller, Delete, Get, Patch, Post } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import {
ICampusFindOneResultDto,
Expand All @@ -8,6 +8,7 @@ import {
} from '../../../../domain';
import {
DtoOperationCreate,
DtoOperationDelete,
DtoOperationFindAll,
DtoOperationFindOne,
DtoOperationUpdate,
Expand Down Expand Up @@ -58,7 +59,7 @@ export class CampusController {

//

@Put('/:id')
@Patch('/:id')
@DtoOperationUpdate(CampusOperations.CAMPUS_UPDATE)
async campusUpdate(
@ResolveRequestContextHttp() requestContext: IRequestContext,
Expand All @@ -76,4 +77,16 @@ export class CampusController {
}

//

@Delete('/:id')
@DtoOperationDelete(CampusOperations.CAMPUS_DELETE_ONE_BY_ID)
async campusDeleteOneById(
@ResolveRequestContextHttp() requestContext: IRequestContext,
@HttpDtoParam(CampusOperations.CAMPUS_DELETE_ONE_BY_ID, 'id')
id: string,
) {
return this.campusService.campusDeleteOneById(requestContext, { id });
}

//
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Resolver } from '@nestjs/graphql';
import {
ICampusDeleteOneByIdInputDto,
ICampusFindOneByIdInputDto,
ICampusInputDto,
ICampusUpdateDto,
Expand Down Expand Up @@ -58,4 +59,13 @@ export class CampusResolver {
) {
return this.campusService.campusUpdate(requestContext, dto);
}

@DtoOperationGqlMutation(CampusOperations.CAMPUS_DELETE_ONE_BY_ID)
async campusDeleteOneById(
@ResolveRequestContextGraphQl() requestContext: IRequestContext,
@GqlDtoInput(CampusOperations.CAMPUS_DELETE_ONE_BY_ID)
dto: ICampusDeleteOneByIdInputDto,
) {
return this.campusService.campusDeleteOneById(requestContext, dto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { get, pick } from 'lodash';
import { SelectQueryBuilder } from 'typeorm';
import { ICampusFindOneByIdInputDto } from '../../(dtos)';
import {
ICampusDeleteOneByIdInputDto,
ICampusFindOneResultDto,
ICampusInputDto,
ICampusUpdateDto,
Expand Down Expand Up @@ -189,4 +190,29 @@ export class CampusService {

return this.campusFindByIdStrict(requestContext, { id: campus.id });
}

//

async campusDeleteOneById(
requestContext: IRequestContext,
dto: ICampusDeleteOneByIdInputDto,
) {
const campus = await this.campusFindByIdStrict(requestContext, dto);

await requestContext.authz.ensurePermission('delete', 'campus', { dto });

if (campus) {
await this.campusRepository
.createQueryBuilder('campus')
.update()
.set({
dateDeleted: 'NOW()',
})
.where('id = :campusId', { campusId: campus.id })
.andWhere('dateDeleted IS NULL')
.execute();
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { InputType } from '@nestjs/graphql';
import * as yup from 'yup';
import { ICampusDeleteOneByIdInputDto } from '../../../(dtos)';
import {
DtoProperty,
createValidationContract,
getSchemaField,
} from '../../../../../infrastructure';
import { CampusDtoProperties, CampusDtoValidationContract } from './campus.dto';

// ======================================================

export const CampusDeleteOneByIdInputValidationContract =
createValidationContract(() =>
yup.object().shape({
id: getSchemaField(CampusDtoValidationContract(), 'id'),
}),
);

// ======================================================

@InputType('CampusDeleteOneByIdInputDto')
export class CampusDeleteOneByIdInputDto
implements ICampusDeleteOneByIdInputDto
{
@DtoProperty(CampusDtoProperties.CAMPUS_ID)
id!: string;
}

// ======================================================
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {
CampusDeleteOneByIdInputDto,
CampusDeleteOneByIdInputValidationContract,
CampusFindOneByIdInputDto,
CampusFindOneByIdInputValidationContract,
} from '.';
Expand Down Expand Up @@ -114,4 +116,33 @@ export const CampusOperations = {
returnType: CampusFindOneResultDto,
},
}),

// ===============================

CAMPUS_DELETE_ONE_BY_ID: createDtoOperationOptions({
description: 'Realiza a remoção de um campus por ID.',

gql: {
name: 'campusDeleteOneById',

inputDtoType: () => CampusDeleteOneByIdInputDto,
inputDtoValidationContract: CampusDeleteOneByIdInputValidationContract,

returnType: () => Boolean,
},

swagger: {
returnType: 'boolean',

params: [
{
name: 'id',
description: 'ID do campus.',
validationContract: ValidationContractUuid,
},
],
},
}),

// ===============================
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export * from './campus-create.input.dto';
export * from './campus-delete-one-by-id.input.dto';
export * from './campus-find-one-by-id.input.dto';
export * from './campus-find-one.result.dto';
export * from './campus-input.dto';
export * from './campus-update.input.dto';
export * from './campus.dto';
export * from './campus.operations';
1 change: 1 addition & 0 deletions luna-backend/src/infrastructure/adapters/RequestContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class RequestContextAuthz implements IRequestContextAuthz {
if (targetStatement) {
if (
targetStatement.action === 'create' ||
targetStatement.action === 'delete' ||
targetStatement.action === 'update'
) {
const can = await targetStatement.check(context, this.#requestContext);
Expand Down
18 changes: 18 additions & 0 deletions luna-backend/src/infrastructure/api-documentate/DtoOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,24 @@ export const DtoOperationUpdate = (options: IDtoOperationOptions) => {

// ==============================================================

export const DtoOperationDelete = (options: IDtoOperationOptions) => {
return applyDecorators(
DtoOperationCommon(options),

ApiResponse({
status: 200,
type: options.swagger.returnType,
description: options.description ?? 'Registro marcado como apagado.',
}),

ApiResponse({
status: 404,
description: 'Registro não encontrado.',
}),
);
};
// ==============================================================

export const DtoOperationGqlQuery = (options: IDtoOperationOptions) => {
return Query(options.gql.returnType, {
name: options.gql.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,15 @@ export type IAuthzStatementBaseUpdate<
check: (context: Context, requestContext: RequestContext) => Promise<boolean>;
};

export type IAuthzStatementBaseDelete<
Target extends string,
Context = unknown,
RequestContext extends IRequestContext = IRequestContext,
> = {
action: 'delete';
target: Target;

check: (context: Context, requestContext: RequestContext) => Promise<boolean>;
};

// ================================================================
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { ICampusInputDto, ICampusUpdateDto } from '../../../../../domain';
import {
ICampusDeleteOneByIdInputDto,
ICampusInputDto,
ICampusUpdateDto,
} from '../../../../../domain';
import { CampusEntity } from '../../../../integrate-database/typeorm/entities/ambientes/campus.entity';
import {
IAuthzStatementBaseCreate,
IAuthzStatementBaseDelete,
IAuthzStatementBaseFind,
IAuthzStatementBaseUpdate,
} from '../../base/IAuthzStatementBase';
Expand All @@ -23,9 +28,15 @@ export type IAuthzStatementCampusUpdate = IAuthzStatementBaseUpdate<
{ dto: ICampusUpdateDto }
>;

export type IAuthzStatementCampusDelete = IAuthzStatementBaseDelete<
'campus',
{ dto: ICampusDeleteOneByIdInputDto }
>;

// ==================================================================

export type IAuthzStatementCampus =
| IAuthzStatementCampusFind
| IAuthzStatementCampusCreate
| IAuthzStatementCampusUpdate;
| IAuthzStatementCampusUpdate
| IAuthzStatementCampusDelete;

0 comments on commit c248a7a

Please sign in to comment.