@goweekdays/core 0.0.12 → 0.0.13
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.
- package/CHANGELOG.md +6 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +25 -17
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +25 -17
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1205,9 +1205,9 @@ type TPromoCode = {
|
|
|
1205
1205
|
declare function MPromoCode(data: TPromoCode): TPromoCode;
|
|
1206
1206
|
|
|
1207
1207
|
declare function usePromoCodeRepo(): {
|
|
1208
|
-
createIndex: () => void
|
|
1209
|
-
createUniqueIndex: () => void
|
|
1210
|
-
add: (value: TPromoCode) => void
|
|
1208
|
+
createIndex: () => Promise<void>;
|
|
1209
|
+
createUniqueIndex: () => Promise<void>;
|
|
1210
|
+
add: (value: TPromoCode) => Promise<void>;
|
|
1211
1211
|
getByCode: (code: string) => Promise<mongodb.WithId<bson.Document> | null>;
|
|
1212
1212
|
getPromoCodes: ({ search, page, limit, sort, status, type, }?: {
|
|
1213
1213
|
search?: string | undefined;
|
package/dist/index.js
CHANGED
|
@@ -21244,24 +21244,29 @@ function usePromoCodeRepo() {
|
|
|
21244
21244
|
throw new import_utils72.InternalServerError("Unable to connect to server.");
|
|
21245
21245
|
}
|
|
21246
21246
|
const collection = db.collection("promo-codes");
|
|
21247
|
-
function createIndex() {
|
|
21247
|
+
async function createIndex() {
|
|
21248
21248
|
try {
|
|
21249
|
-
collection.createIndexes([
|
|
21249
|
+
await collection.createIndexes([
|
|
21250
|
+
{ key: { code: 1 } },
|
|
21251
|
+
{ key: { type: 1 } }
|
|
21252
|
+
]);
|
|
21250
21253
|
} catch (error) {
|
|
21251
21254
|
throw new Error("Failed to create index for promo code.");
|
|
21252
21255
|
}
|
|
21253
21256
|
}
|
|
21254
|
-
function createUniqueIndex() {
|
|
21257
|
+
async function createUniqueIndex() {
|
|
21255
21258
|
try {
|
|
21256
|
-
collection.createIndexes([
|
|
21259
|
+
await collection.createIndexes([
|
|
21260
|
+
{ key: { code: 1, type: 1 }, unique: true }
|
|
21261
|
+
]);
|
|
21257
21262
|
} catch (error) {
|
|
21258
21263
|
throw new Error("Failed to create unique index for promo code.");
|
|
21259
21264
|
}
|
|
21260
21265
|
}
|
|
21261
|
-
function add(value) {
|
|
21266
|
+
async function add(value) {
|
|
21262
21267
|
try {
|
|
21263
21268
|
value = MPromoCode(value);
|
|
21264
|
-
collection.insertOne(value);
|
|
21269
|
+
await collection.insertOne(value);
|
|
21265
21270
|
} catch (error) {
|
|
21266
21271
|
import_utils72.logger.log({ level: "error", message: `${error}` });
|
|
21267
21272
|
const isDuplicated = error.message.includes("duplicate");
|
|
@@ -21271,7 +21276,7 @@ function usePromoCodeRepo() {
|
|
|
21271
21276
|
throw new import_utils72.InternalServerError("Internal server error.");
|
|
21272
21277
|
}
|
|
21273
21278
|
}
|
|
21274
|
-
function getByCode(code) {
|
|
21279
|
+
async function getByCode(code) {
|
|
21275
21280
|
const schema3 = import_joi22.default.object({
|
|
21276
21281
|
code: import_joi22.default.string().trim().required()
|
|
21277
21282
|
});
|
|
@@ -21280,7 +21285,7 @@ function usePromoCodeRepo() {
|
|
|
21280
21285
|
throw new import_utils72.BadRequestError(error.message);
|
|
21281
21286
|
}
|
|
21282
21287
|
try {
|
|
21283
|
-
return collection.findOne({ code });
|
|
21288
|
+
return await collection.findOne({ code });
|
|
21284
21289
|
} catch (error2) {
|
|
21285
21290
|
throw new import_utils72.InternalServerError("Internal server error.");
|
|
21286
21291
|
}
|
|
@@ -21342,7 +21347,7 @@ function usePromoCodeController() {
|
|
|
21342
21347
|
return;
|
|
21343
21348
|
}
|
|
21344
21349
|
try {
|
|
21345
|
-
_add(data);
|
|
21350
|
+
await _add(data);
|
|
21346
21351
|
res.json({ message: "Successfully added promo code." });
|
|
21347
21352
|
return;
|
|
21348
21353
|
} catch (error2) {
|
|
@@ -21362,7 +21367,7 @@ function usePromoCodeController() {
|
|
|
21362
21367
|
return;
|
|
21363
21368
|
}
|
|
21364
21369
|
try {
|
|
21365
|
-
const promoCode = _getByCode(code);
|
|
21370
|
+
const promoCode = await _getByCode(code);
|
|
21366
21371
|
res.json({ promoCode });
|
|
21367
21372
|
return;
|
|
21368
21373
|
} catch (error2) {
|
|
@@ -21374,22 +21379,25 @@ function usePromoCodeController() {
|
|
|
21374
21379
|
}
|
|
21375
21380
|
}
|
|
21376
21381
|
async function getPromoCodes(req, res, next) {
|
|
21377
|
-
const page = req.query.page;
|
|
21378
|
-
const limit = req.query.limit;
|
|
21382
|
+
const page = req.query.page ? Number(req.query.page) : 1;
|
|
21383
|
+
const limit = req.query.limit ? Number(req.query.limit) : 10;
|
|
21379
21384
|
const search = req.query.search;
|
|
21380
21385
|
const status = req.query.status;
|
|
21381
21386
|
const validation = import_joi23.default.object({
|
|
21382
21387
|
page: import_joi23.default.number().required(),
|
|
21383
|
-
limit: import_joi23.default.number().
|
|
21388
|
+
limit: import_joi23.default.number().integer().optional().min(10).max(100),
|
|
21389
|
+
search: import_joi23.default.string().optional().allow(""),
|
|
21390
|
+
status: import_joi23.default.string().required()
|
|
21384
21391
|
});
|
|
21385
|
-
const { error } = validation.validate({ page, limit });
|
|
21392
|
+
const { error } = validation.validate({ page, limit, search, status });
|
|
21386
21393
|
if (error) {
|
|
21387
21394
|
next(new import_utils73.BadRequestError(error.message));
|
|
21395
|
+
return;
|
|
21388
21396
|
}
|
|
21389
21397
|
try {
|
|
21390
|
-
const promoCodes = _getPromoCodes({
|
|
21391
|
-
page
|
|
21392
|
-
limit
|
|
21398
|
+
const promoCodes = await _getPromoCodes({
|
|
21399
|
+
page,
|
|
21400
|
+
limit,
|
|
21393
21401
|
search,
|
|
21394
21402
|
status
|
|
21395
21403
|
});
|