@vasrefil/api-toolkit 1.7.8 → 1.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import * as joi from
|
|
2
|
-
import { RootService } from
|
|
3
|
-
import { Request, Response, NextFunction } from
|
|
1
|
+
import * as joi from "joi";
|
|
2
|
+
import { RootService } from "../services/_root.service";
|
|
3
|
+
import { Request, Response, NextFunction } from "express";
|
|
4
4
|
/**
|
|
5
5
|
* Validation middleware that uses joi to validate the request body.
|
|
6
6
|
* @param schema Joi schema to use to validate the request body
|
|
7
7
|
*/
|
|
8
8
|
export declare class JoiVdtor_ extends RootService {
|
|
9
|
-
vdtor(schema: joi.Schema, field?:
|
|
9
|
+
vdtor(schema: joi.Schema, field?: "body" | "query", showErrorMessage?: boolean): (req: Request, res: Response, next: NextFunction) => Promise<any>;
|
|
10
10
|
}
|
|
11
11
|
declare const Joi: JoiVdtor_;
|
|
12
12
|
export { Joi };
|
|
@@ -9,22 +9,32 @@ const { FAILED_VALIDATION } = status_interface_1.Status;
|
|
|
9
9
|
* @param schema Joi schema to use to validate the request body
|
|
10
10
|
*/
|
|
11
11
|
class JoiVdtor_ extends _root_service_1.RootService {
|
|
12
|
-
vdtor(schema, field =
|
|
12
|
+
vdtor(schema, field = "body", showErrorMessage = false) {
|
|
13
13
|
return async (req, res, next) => {
|
|
14
|
-
const actionType =
|
|
14
|
+
const actionType = "SCHEMA_VALIDATION";
|
|
15
15
|
try {
|
|
16
|
-
const validated_data = await schema.validateAsync(req[field], {
|
|
16
|
+
const validated_data = await schema.validateAsync(req[field], {
|
|
17
|
+
abortEarly: false,
|
|
18
|
+
});
|
|
17
19
|
req[field] = validated_data;
|
|
18
20
|
next();
|
|
19
21
|
}
|
|
20
22
|
catch (err) {
|
|
21
|
-
let message = 'Some validation errors occured';
|
|
22
23
|
const errorDetails = err && err.details && err.details;
|
|
23
|
-
|
|
24
|
+
const errMessage = err?.details[0] ?? err?.details[0].message;
|
|
25
|
+
let message = showErrorMessage ? errMessage : "Some validation errors occured";
|
|
26
|
+
return this.sendResponse({
|
|
27
|
+
req,
|
|
28
|
+
res,
|
|
29
|
+
status: FAILED_VALIDATION,
|
|
30
|
+
actionType,
|
|
31
|
+
message,
|
|
32
|
+
error: errorDetails,
|
|
33
|
+
});
|
|
24
34
|
}
|
|
25
35
|
};
|
|
26
36
|
}
|
|
27
37
|
}
|
|
28
38
|
exports.JoiVdtor_ = JoiVdtor_;
|
|
29
|
-
const Joi = new JoiVdtor_;
|
|
39
|
+
const Joi = new JoiVdtor_();
|
|
30
40
|
exports.Joi = Joi;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { BulkJobOptions } from 'bullmq';
|
|
1
2
|
export interface SendEmailPayloadI {
|
|
2
3
|
action_type: string;
|
|
3
4
|
from: string;
|
|
@@ -8,7 +9,7 @@ export interface SendEmailPayloadI {
|
|
|
8
9
|
template_code?: string;
|
|
9
10
|
}
|
|
10
11
|
declare class EmailUtil_ {
|
|
11
|
-
send_email: (payload: SendEmailPayloadI) => Promise<void>;
|
|
12
|
+
send_email: (payload: SendEmailPayloadI[], job_option?: BulkJobOptions) => Promise<void>;
|
|
12
13
|
}
|
|
13
14
|
export declare const EmailUtil: EmailUtil_;
|
|
14
15
|
export {};
|
|
@@ -5,11 +5,14 @@ const queue_1 = require("./../background-jobs/queue");
|
|
|
5
5
|
const helpers_1 = require("../helpers");
|
|
6
6
|
class EmailUtil_ {
|
|
7
7
|
constructor() {
|
|
8
|
-
this.send_email = async (payload) => {
|
|
8
|
+
this.send_email = async (payload, job_option = {}) => {
|
|
9
9
|
try {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
const jobs = payload.map(p => ({
|
|
11
|
+
name: p.action_type,
|
|
12
|
+
data: p,
|
|
13
|
+
opts: job_option
|
|
14
|
+
}));
|
|
15
|
+
(0, queue_1.EmailQueue)().addBulk(jobs);
|
|
13
16
|
}
|
|
14
17
|
catch (error) {
|
|
15
18
|
const err = helpers_1.RequestHelper.get_error(error);
|