|
| 1 | +import { EntityClass, ObjectLiteral } from '../../../../types'; |
| 2 | +import { ApiBody, ApiOperation, ApiParam, ApiResponse } from '@nestjs/swagger'; |
| 3 | +import { Type } from '@nestjs/common'; |
| 4 | + |
| 5 | +import { EntityProps, TypeField, ZodParams } from '../../types'; |
| 6 | +import { zodPatchRelationship } from '../../zod'; |
| 7 | +import { errorSchema } from '../utils'; |
| 8 | +import { generateSchema } from '@anatine/zod-openapi'; |
| 9 | +import { |
| 10 | + ReferenceObject, |
| 11 | + SchemaObject, |
| 12 | +} from '@nestjs/swagger/dist/interfaces/open-api-spec.interface'; |
| 13 | + |
| 14 | +export function deleteRelationship<E extends ObjectLiteral>( |
| 15 | + controller: Type<any>, |
| 16 | + descriptor: PropertyDescriptor, |
| 17 | + entity: EntityClass<E>, |
| 18 | + zodParams: ZodParams<E, EntityProps<E>, string>, |
| 19 | + methodName: string |
| 20 | +) { |
| 21 | + const entityName = entity.name; |
| 22 | + |
| 23 | + const { |
| 24 | + entityFieldsStructure: { relations }, |
| 25 | + typeId, |
| 26 | + } = zodParams; |
| 27 | + |
| 28 | + ApiOperation({ |
| 29 | + summary: `Delete list of relation for resource "${entityName}"`, |
| 30 | + operationId: `${controller.name}_${methodName}`, |
| 31 | + })(controller, methodName, descriptor); |
| 32 | + |
| 33 | + ApiParam({ |
| 34 | + name: 'id', |
| 35 | + required: true, |
| 36 | + type: typeId === TypeField.number ? 'integer' : 'string', |
| 37 | + description: `ID of resource "${entityName}"`, |
| 38 | + })(controller, methodName, descriptor); |
| 39 | + |
| 40 | + ApiParam({ |
| 41 | + name: 'relName', |
| 42 | + required: true, |
| 43 | + type: 'string', |
| 44 | + enum: relations, |
| 45 | + description: `Relation name of resource "${entityName}"`, |
| 46 | + })(controller, methodName, descriptor); |
| 47 | + |
| 48 | + ApiBody({ |
| 49 | + description: `Json api schema for delete "${entityName}" item`, |
| 50 | + schema: generateSchema(zodPatchRelationship) as |
| 51 | + | SchemaObject |
| 52 | + | ReferenceObject, |
| 53 | + required: true, |
| 54 | + })(controller, methodName, descriptor); |
| 55 | + |
| 56 | + ApiResponse({ |
| 57 | + status: 400, |
| 58 | + description: 'Wrong url parameters', |
| 59 | + schema: errorSchema, |
| 60 | + })(controller, methodName, descriptor); |
| 61 | + |
| 62 | + ApiResponse({ |
| 63 | + status: 422, |
| 64 | + description: 'Incorrect type for relation', |
| 65 | + schema: errorSchema, |
| 66 | + })(controller, methodName, descriptor); |
| 67 | + |
| 68 | + ApiResponse({ |
| 69 | + status: 404, |
| 70 | + description: 'Resource not found ', |
| 71 | + schema: errorSchema, |
| 72 | + })(controller, methodName, descriptor); |
| 73 | + |
| 74 | + ApiResponse({ |
| 75 | + status: 204, |
| 76 | + description: `Item/s of relation for "${entityName}" has been deleted`, |
| 77 | + })(controller, methodName, descriptor); |
| 78 | +} |
0 commit comments