Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@secjs/base",
"version": "1.1.8",
"version": "1.1.9",
"license": "MIT",
"author": "João Lenon",
"repository": "https://github.com/SecJS/Base.git",
Expand Down
75 changes: 70 additions & 5 deletions repositories/PrismaRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,20 @@ export abstract class PrismaRepository<TModel> {
const value = where[key]

if (!isInternRequest && !this.wheres?.includes(key)) {
throw new Error(`It is not possible to filter by ${key}`)
const status = 400
const message = `It is not possible to filter by ${key}`

throw {
status,
stack: new Error(message),
name: 'REPOSITORY_WHERE_ERROR',
message: {
message,
statusCode: status,
error: 'Bad Request',
},
isSecJsException: true,
}
}

if (value === 'null') {
Expand Down Expand Up @@ -104,7 +117,20 @@ export abstract class PrismaRepository<TModel> {

includes.forEach(i => {
if (!isInternRequest && !this.relations?.includes(i.relation)) {
throw new Error(`It is not possible to include ${i.relation} relation`)
const status = 400
const message = `It is not possible to include ${i.relation} relation`

throw {
status,
stack: new Error(message),
name: 'REPOSITORY_RELATION_ERROR',
message: {
message,
statusCode: status,
error: 'Bad Request',
},
isSecJsException: true,
}
}

include[i.relation] = this.factoryRequest(i)
Expand Down Expand Up @@ -189,7 +215,20 @@ export abstract class PrismaRepository<TModel> {
model = await this.getOne(id)

if (!model) {
throw new Error('MODEL_NOT_FOUND_UPDATE')
const status = 404
const message = 'The model id has not been found to update.'

throw {
status,
stack: new Error(message),
name: 'MODEL_NOT_FOUND_UPDATE',
message: {
message,
statusCode: status,
error: 'Not Found',
},
isSecJsException: true,
}
}
}

Expand All @@ -211,13 +250,39 @@ export abstract class PrismaRepository<TModel> {
model = await this.getOne(id)

if (!model) {
throw new Error('MODEL_NOT_FOUND_DELETE')
const status = 404
const message = 'The model id has not been found to delete.'

throw {
status,
stack: new Error(message),
name: 'MODEL_NOT_FOUND_DELETE',
message: {
message,
statusCode: status,
error: 'Not Found',
},
isSecJsException: true,
}
}
}

if (soft) {
if (model.deletedAt) {
throw new Error('MODEL_IS_ALREADY_DELETED')
const status = 400
const message = 'The model id has been already deleted.'

throw {
status,
stack: new Error(message),
name: 'MODEL_ALREADY_DELETED',
message: {
message,
statusCode: status,
error: 'Bad Request',
},
isSecJsException: true,
}
}

return this.updateOne(model, { deletedAt: new Date() })
Expand Down