@konplit-services/common 1.0.13 → 1.0.14
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,8 +1,8 @@
|
|
|
1
|
-
import { Document, Model } from
|
|
1
|
+
import { Document, Model } from "mongoose";
|
|
2
2
|
export interface PaginationOptions<T extends Document> {
|
|
3
3
|
page?: number;
|
|
4
4
|
limit?: number;
|
|
5
|
-
sort?: Record<string,
|
|
5
|
+
sort?: Record<string, "asc" | "desc">;
|
|
6
6
|
select?: string | Record<string, any>;
|
|
7
7
|
query?: Record<string, any>;
|
|
8
8
|
}
|
|
@@ -17,3 +17,25 @@ export interface PaginationResult<T extends Document> {
|
|
|
17
17
|
results: T[];
|
|
18
18
|
}
|
|
19
19
|
export declare function paginate<T extends Document>(model: Model<T>, options?: PaginationOptions<T>): Promise<PaginationResult<T>>;
|
|
20
|
+
export interface FilterOptions {
|
|
21
|
+
search?: string;
|
|
22
|
+
status?: string;
|
|
23
|
+
from?: string;
|
|
24
|
+
to?: string;
|
|
25
|
+
limit?: string;
|
|
26
|
+
page?: string;
|
|
27
|
+
sort_by?: string;
|
|
28
|
+
order?: "asc" | "desc";
|
|
29
|
+
merchantId?: string;
|
|
30
|
+
accountId?: string;
|
|
31
|
+
[key: string]: any;
|
|
32
|
+
}
|
|
33
|
+
export declare const generateQueryObject: (filters: FilterOptions, searchesFieldNames?: string[], select?: string | Record<string, any>) => {
|
|
34
|
+
page: number;
|
|
35
|
+
limit: number;
|
|
36
|
+
query: any;
|
|
37
|
+
sort: {
|
|
38
|
+
[key: string]: 1 | -1;
|
|
39
|
+
};
|
|
40
|
+
select: string | Record<string, any>;
|
|
41
|
+
};
|
|
@@ -9,15 +9,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.paginate = void 0;
|
|
12
|
+
exports.generateQueryObject = exports.paginate = void 0;
|
|
13
13
|
function paginate(model, options = {}) {
|
|
14
14
|
return __awaiter(this, void 0, void 0, function* () {
|
|
15
15
|
const page = options.page || 1;
|
|
16
|
-
const limit = options.limit ||
|
|
16
|
+
const limit = options.limit || 20;
|
|
17
17
|
const skip = (page - 1) * limit;
|
|
18
18
|
const query = options.query || {};
|
|
19
19
|
const sort = options.sort || {};
|
|
20
|
-
const select = options.select ||
|
|
20
|
+
const select = options.select || "";
|
|
21
21
|
const keys = Object.keys(query);
|
|
22
22
|
let totalDocs;
|
|
23
23
|
if (keys.length > 0) {
|
|
@@ -48,3 +48,70 @@ function paginate(model, options = {}) {
|
|
|
48
48
|
});
|
|
49
49
|
}
|
|
50
50
|
exports.paginate = paginate;
|
|
51
|
+
const generateQueryObject = (filters, searchesFieldNames = [], select = "") => {
|
|
52
|
+
const { status, limit, page, sort_by = "createdAt", order = "desc", merchantId, accountId, } = filters;
|
|
53
|
+
const excludes = [
|
|
54
|
+
"status",
|
|
55
|
+
"from",
|
|
56
|
+
"page",
|
|
57
|
+
"to",
|
|
58
|
+
"limit",
|
|
59
|
+
"sort_by",
|
|
60
|
+
"search",
|
|
61
|
+
"accountId",
|
|
62
|
+
"accountId",
|
|
63
|
+
"order",
|
|
64
|
+
];
|
|
65
|
+
const query = {};
|
|
66
|
+
let sort = {};
|
|
67
|
+
const keys = Object.keys(filters);
|
|
68
|
+
for (const key in keys) {
|
|
69
|
+
if (!excludes.includes(key)) {
|
|
70
|
+
query[key] = filters[key];
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (filters.merchantId) {
|
|
74
|
+
query.merchantId = merchantId;
|
|
75
|
+
}
|
|
76
|
+
if (filters.accountId) {
|
|
77
|
+
query.accountId = accountId;
|
|
78
|
+
}
|
|
79
|
+
if (filters.search) {
|
|
80
|
+
const nameRegex = new RegExp(filters.search, "i");
|
|
81
|
+
const searchArray = [];
|
|
82
|
+
if (searchesFieldNames.length) {
|
|
83
|
+
for (const field of searchesFieldNames) {
|
|
84
|
+
searchArray.push({ [field]: nameRegex });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
query.$or = searchArray;
|
|
88
|
+
}
|
|
89
|
+
if (filters.status) {
|
|
90
|
+
query.status = status;
|
|
91
|
+
}
|
|
92
|
+
if (filters.from && filters.to) {
|
|
93
|
+
query.createdAt = {
|
|
94
|
+
$gte: new Date(filters.from),
|
|
95
|
+
$lte: new Date(filters.to),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
else if (filters.from) {
|
|
99
|
+
query.createdAt = {
|
|
100
|
+
$gte: new Date(filters.from),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
if (filters.sort_by && filters.order) {
|
|
104
|
+
const newSory = {
|
|
105
|
+
[sort_by]: order === "asc" ? 1 : -1,
|
|
106
|
+
};
|
|
107
|
+
sort = Object.assign({}, newSory);
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
page: parseInt(page, 10),
|
|
111
|
+
limit: parseInt(limit, 10),
|
|
112
|
+
query: query,
|
|
113
|
+
sort: sort,
|
|
114
|
+
select: select,
|
|
115
|
+
};
|
|
116
|
+
};
|
|
117
|
+
exports.generateQueryObject = generateQueryObject;
|