@recursyve/nestjs-unique-codes-sequelize 8.0.0-beta.5 → 8.0.0-beta.7
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/package.json
CHANGED
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
import { UniqueCodes, UniqueCodesService } from "@recursyve/nestjs-unique-codes-core";
|
|
2
|
+
import { Transaction } from "sequelize";
|
|
2
3
|
import { SequelizeUniqueCodes } from "../models";
|
|
3
|
-
|
|
4
|
+
interface SequelizeUniqueCodeOptions {
|
|
5
|
+
transaction?: Transaction;
|
|
6
|
+
}
|
|
7
|
+
export declare class SequelizeUniqueCodesService extends UniqueCodesService<SequelizeUniqueCodeOptions> {
|
|
4
8
|
private readonly repository;
|
|
5
9
|
constructor(repository: typeof SequelizeUniqueCodes);
|
|
6
|
-
create(uniqueCode: Partial<UniqueCodes
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
create(uniqueCode: Partial<UniqueCodes>, options?: SequelizeUniqueCodeOptions): Promise<UniqueCodes>;
|
|
11
|
+
bulkCreate(uniqueCodes: Partial<UniqueCodes>[], options?: SequelizeUniqueCodeOptions): Promise<UniqueCodes[]>;
|
|
12
|
+
getByCode(code: string, options?: SequelizeUniqueCodeOptions): Promise<UniqueCodes>;
|
|
13
|
+
findByCode(code: string, options?: SequelizeUniqueCodeOptions): Promise<UniqueCodes | null>;
|
|
14
|
+
getByCodeAndType(code: string, type: string, options?: SequelizeUniqueCodeOptions): Promise<UniqueCodes>;
|
|
15
|
+
findByCodeAndType(code: string, type: string, options?: SequelizeUniqueCodeOptions): Promise<UniqueCodes | null>;
|
|
16
|
+
findFromMetadata(metadata: Record<string, any>, options: SequelizeUniqueCodeOptions | undefined): Promise<UniqueCodes | null>;
|
|
17
|
+
findFromMetadataAndType(metadata: Record<string, any>, type: any, options: SequelizeUniqueCodeOptions | undefined): Promise<UniqueCodes | null>;
|
|
18
|
+
validate(uniqueCode: string | UniqueCodes, options?: SequelizeUniqueCodeOptions): Promise<boolean>;
|
|
19
|
+
use(id: number, metadata?: Record<string, any>, options?: SequelizeUniqueCodeOptions): Promise<void>;
|
|
13
20
|
}
|
|
21
|
+
export {};
|
|
@@ -2,59 +2,86 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SequelizeUniqueCodesService = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
+
const common_1 = require("@nestjs/common");
|
|
5
6
|
const sequelize_1 = require("@nestjs/sequelize");
|
|
6
7
|
const nestjs_unique_codes_core_1 = require("@recursyve/nestjs-unique-codes-core");
|
|
7
|
-
const models_1 = require("../models");
|
|
8
|
-
const common_1 = require("@nestjs/common");
|
|
9
8
|
const date_fns_1 = require("date-fns");
|
|
9
|
+
const sequelize_2 = require("sequelize");
|
|
10
|
+
const sequelize_typescript_1 = require("sequelize-typescript");
|
|
11
|
+
const models_1 = require("../models");
|
|
10
12
|
let SequelizeUniqueCodesService = class SequelizeUniqueCodesService extends nestjs_unique_codes_core_1.UniqueCodesService {
|
|
11
13
|
constructor(repository) {
|
|
12
14
|
super();
|
|
13
15
|
this.repository = repository;
|
|
14
16
|
}
|
|
15
|
-
create(uniqueCode) {
|
|
16
|
-
return this.repository.create(uniqueCode);
|
|
17
|
+
create(uniqueCode, options) {
|
|
18
|
+
return this.repository.create(uniqueCode, options);
|
|
19
|
+
}
|
|
20
|
+
bulkCreate(uniqueCodes, options) {
|
|
21
|
+
return this.repository.bulkCreate(uniqueCodes, options);
|
|
17
22
|
}
|
|
18
|
-
async getByCode(code) {
|
|
19
|
-
const uniqueCode = await this.repository.findOne({
|
|
20
|
-
where: {
|
|
23
|
+
async getByCode(code, options) {
|
|
24
|
+
const uniqueCode = await this.repository.findOne(Object.assign({ where: {
|
|
21
25
|
code
|
|
22
|
-
}
|
|
23
|
-
});
|
|
26
|
+
} }, options));
|
|
24
27
|
if (!uniqueCode)
|
|
25
28
|
throw new common_1.NotFoundException(`Unique code '${code}' not found`);
|
|
26
29
|
return uniqueCode;
|
|
27
30
|
}
|
|
28
|
-
findByCode(code) {
|
|
29
|
-
return this.repository.findOne({
|
|
30
|
-
where: {
|
|
31
|
+
findByCode(code, options) {
|
|
32
|
+
return this.repository.findOne(Object.assign({ where: {
|
|
31
33
|
code
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
+
} }, options));
|
|
34
35
|
}
|
|
35
|
-
async getByCodeAndType(code, type) {
|
|
36
|
-
const uniqueCode = await this.repository.findOne({
|
|
37
|
-
where: {
|
|
36
|
+
async getByCodeAndType(code, type, options) {
|
|
37
|
+
const uniqueCode = await this.repository.findOne(Object.assign({ where: {
|
|
38
38
|
code,
|
|
39
39
|
type
|
|
40
|
-
}
|
|
41
|
-
});
|
|
40
|
+
} }, options));
|
|
42
41
|
if (!uniqueCode)
|
|
43
42
|
throw new common_1.NotFoundException(`Unique code '${code}' with type '${type}' not found`);
|
|
44
43
|
return uniqueCode;
|
|
45
44
|
}
|
|
46
|
-
findByCodeAndType(code, type) {
|
|
47
|
-
return this.repository.findOne({
|
|
48
|
-
where: {
|
|
45
|
+
findByCodeAndType(code, type, options) {
|
|
46
|
+
return this.repository.findOne(Object.assign({ where: {
|
|
49
47
|
code,
|
|
50
48
|
type
|
|
49
|
+
} }, options));
|
|
50
|
+
}
|
|
51
|
+
findFromMetadata(metadata, options) {
|
|
52
|
+
const conditions = [];
|
|
53
|
+
const where = {
|
|
54
|
+
[sequelize_2.Op.and]: conditions
|
|
55
|
+
};
|
|
56
|
+
for (const key in metadata) {
|
|
57
|
+
if (!(key in metadata)) {
|
|
58
|
+
continue;
|
|
51
59
|
}
|
|
60
|
+
conditions.push(sequelize_typescript_1.Sequelize.where(sequelize_typescript_1.Sequelize.fn("JSON_EXTRACT", sequelize_typescript_1.Sequelize.col("metadata"), sequelize_typescript_1.Sequelize.literal(`'$.${key}'`)), metadata[key]));
|
|
61
|
+
}
|
|
62
|
+
return this.repository.findOne({
|
|
63
|
+
where
|
|
52
64
|
});
|
|
53
65
|
}
|
|
54
|
-
|
|
66
|
+
findFromMetadataAndType(metadata, type, options) {
|
|
67
|
+
const conditions = [{ type }];
|
|
68
|
+
const where = {
|
|
69
|
+
[sequelize_2.Op.and]: conditions
|
|
70
|
+
};
|
|
71
|
+
for (const key in metadata) {
|
|
72
|
+
if (!(key in metadata)) {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
conditions.push(sequelize_typescript_1.Sequelize.where(sequelize_typescript_1.Sequelize.fn("JSON_EXTRACT", sequelize_typescript_1.Sequelize.col("metadata"), sequelize_typescript_1.Sequelize.literal(`'$.${key}'`)), metadata[key]));
|
|
76
|
+
}
|
|
77
|
+
return this.repository.findOne({
|
|
78
|
+
where
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
async validate(uniqueCode, options) {
|
|
55
82
|
var _a, _b;
|
|
56
83
|
if (typeof uniqueCode === "string") {
|
|
57
|
-
uniqueCode = await this.getByCode(uniqueCode);
|
|
84
|
+
uniqueCode = await this.getByCode(uniqueCode, options);
|
|
58
85
|
}
|
|
59
86
|
if (uniqueCode.availableUsageCount <= 0) {
|
|
60
87
|
return false;
|
|
@@ -65,25 +92,23 @@ let SequelizeUniqueCodesService = class SequelizeUniqueCodesService extends nest
|
|
|
65
92
|
end: (_b = uniqueCode.expiresAt) !== null && _b !== void 0 ? _b : now
|
|
66
93
|
});
|
|
67
94
|
}
|
|
68
|
-
async use(id, metadata) {
|
|
95
|
+
async use(id, metadata, options) {
|
|
69
96
|
var _a;
|
|
70
|
-
const uniqueCode = await this.repository.findByPk(id);
|
|
71
|
-
const isValid = await this.validate(uniqueCode);
|
|
97
|
+
const uniqueCode = await this.repository.findByPk(id, options);
|
|
98
|
+
const isValid = await this.validate(uniqueCode, options);
|
|
72
99
|
if (!isValid) {
|
|
73
100
|
throw new common_1.BadRequestException(`Unique code ${uniqueCode.code} invalid`);
|
|
74
101
|
}
|
|
75
102
|
const usages = (_a = uniqueCode.usages) !== null && _a !== void 0 ? _a : [];
|
|
76
103
|
usages.push(Object.assign(Object.assign({}, metadata), { usedAt: new Date() }));
|
|
77
104
|
if (uniqueCode.availableUsageCount) {
|
|
78
|
-
await uniqueCode.decrement("availableUsageCount");
|
|
105
|
+
await uniqueCode.decrement("availableUsageCount", options);
|
|
79
106
|
}
|
|
80
107
|
await this.repository.update({
|
|
81
108
|
usages
|
|
82
|
-
}, {
|
|
83
|
-
where: {
|
|
109
|
+
}, Object.assign({ where: {
|
|
84
110
|
id
|
|
85
|
-
}
|
|
86
|
-
});
|
|
111
|
+
} }, options));
|
|
87
112
|
}
|
|
88
113
|
};
|
|
89
114
|
SequelizeUniqueCodesService = tslib_1.__decorate([
|