mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 20:30:33 +00:00
feat: deleteIfNoRelations
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
export class ModelHasRelationsError extends Error {
|
||||
type: string;
|
||||
|
||||
constructor(type: string = 'ModelHasRelations', message?: string) {
|
||||
message = message || `Entity has relations`;
|
||||
super(message);
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import {
|
||||
ExceptionFilter,
|
||||
Catch,
|
||||
ArgumentsHost,
|
||||
HttpStatus,
|
||||
} from '@nestjs/common';
|
||||
import { Response } from 'express';
|
||||
import { ModelHasRelationsError } from '../exceptions/ModelHasRelations.exception';
|
||||
|
||||
@Catch(ModelHasRelationsError)
|
||||
export class ModelHasRelationsFilter implements ExceptionFilter {
|
||||
catch(exception: ModelHasRelationsError, host: ArgumentsHost) {
|
||||
const ctx = host.switchToHttp();
|
||||
const response = ctx.getResponse<Response>();
|
||||
const status = HttpStatus.CONFLICT;
|
||||
|
||||
response.status(status).json({
|
||||
errors: [
|
||||
{
|
||||
statusCode: status,
|
||||
type: exception.type || 'MODEL_HAS_RELATIONS',
|
||||
message: exception.message,
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
}
|
||||
7
packages/server/src/common/types/Objection.d.ts
vendored
Normal file
7
packages/server/src/common/types/Objection.d.ts
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
import { QueryBuilder, Model } from 'objection';
|
||||
|
||||
declare module 'objection' {
|
||||
interface QueryBuilder<M extends Model, R = M[]> {
|
||||
deleteIfNoRelations(this: QueryBuilder<M, R>, ...args: any[]): Promise<any>;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user