@recursyve/nestjs-unique-codes-sequelize 8.0.0-beta.4 → 8.0.0-beta.6

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@recursyve/nestjs-unique-codes-sequelize",
3
- "version": "8.0.0-beta.4",
3
+ "version": "8.0.0-beta.6",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -1,13 +1,19 @@
1
1
  import { UniqueCodes, UniqueCodesService } from "@recursyve/nestjs-unique-codes-core";
2
2
  import { SequelizeUniqueCodes } from "../models";
3
- export declare class SequelizeUniqueCodesService extends UniqueCodesService {
3
+ import { Transaction } from "sequelize";
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>): Promise<UniqueCodes>;
7
- getByCode(code: string): Promise<UniqueCodes>;
8
- findByCode(code: string): Promise<UniqueCodes | null>;
9
- getByCodeAndType(code: string, type: string): Promise<UniqueCodes>;
10
- findByCodeAndType(code: string, type: string): Promise<UniqueCodes | null>;
11
- validate(uniqueCode: string | UniqueCodes): Promise<boolean>;
12
- use(id: number, metadata?: Record<string, any>): Promise<void>;
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
+ validate(uniqueCode: string | UniqueCodes, options?: SequelizeUniqueCodeOptions): Promise<boolean>;
17
+ use(id: number, metadata?: Record<string, any>, options?: SequelizeUniqueCodeOptions): Promise<void>;
13
18
  }
19
+ export {};
@@ -12,49 +12,44 @@ let SequelizeUniqueCodesService = class SequelizeUniqueCodesService extends nest
12
12
  super();
13
13
  this.repository = repository;
14
14
  }
15
- create(uniqueCode) {
16
- return this.repository.create(uniqueCode);
15
+ create(uniqueCode, options) {
16
+ return this.repository.create(uniqueCode, options);
17
17
  }
18
- async getByCode(code) {
19
- const uniqueCode = await this.repository.findOne({
20
- where: {
18
+ bulkCreate(uniqueCodes, options) {
19
+ return this.repository.bulkCreate(uniqueCodes, options);
20
+ }
21
+ async getByCode(code, options) {
22
+ const uniqueCode = await this.repository.findOne(Object.assign({ where: {
21
23
  code
22
- }
23
- });
24
+ } }, options));
24
25
  if (!uniqueCode)
25
26
  throw new common_1.NotFoundException(`Unique code '${code}' not found`);
26
27
  return uniqueCode;
27
28
  }
28
- findByCode(code) {
29
- return this.repository.findOne({
30
- where: {
29
+ findByCode(code, options) {
30
+ return this.repository.findOne(Object.assign({ where: {
31
31
  code
32
- }
33
- });
32
+ } }, options));
34
33
  }
35
- async getByCodeAndType(code, type) {
36
- const uniqueCode = await this.repository.findOne({
37
- where: {
34
+ async getByCodeAndType(code, type, options) {
35
+ const uniqueCode = await this.repository.findOne(Object.assign({ where: {
38
36
  code,
39
37
  type
40
- }
41
- });
38
+ } }, options));
42
39
  if (!uniqueCode)
43
40
  throw new common_1.NotFoundException(`Unique code '${code}' with type '${type}' not found`);
44
41
  return uniqueCode;
45
42
  }
46
- findByCodeAndType(code, type) {
47
- return this.repository.findOne({
48
- where: {
43
+ findByCodeAndType(code, type, options) {
44
+ return this.repository.findOne(Object.assign({ where: {
49
45
  code,
50
46
  type
51
- }
52
- });
47
+ } }, options));
53
48
  }
54
- async validate(uniqueCode) {
49
+ async validate(uniqueCode, options) {
55
50
  var _a, _b;
56
51
  if (typeof uniqueCode === "string") {
57
- uniqueCode = await this.getByCode(uniqueCode);
52
+ uniqueCode = await this.getByCode(uniqueCode, options);
58
53
  }
59
54
  if (uniqueCode.availableUsageCount <= 0) {
60
55
  return false;
@@ -65,25 +60,23 @@ let SequelizeUniqueCodesService = class SequelizeUniqueCodesService extends nest
65
60
  end: (_b = uniqueCode.expiresAt) !== null && _b !== void 0 ? _b : now
66
61
  });
67
62
  }
68
- async use(id, metadata) {
63
+ async use(id, metadata, options) {
69
64
  var _a;
70
- const uniqueCode = await this.repository.findByPk(id);
71
- const isValid = await this.validate(uniqueCode);
65
+ const uniqueCode = await this.repository.findByPk(id, options);
66
+ const isValid = await this.validate(uniqueCode, options);
72
67
  if (!isValid) {
73
68
  throw new common_1.BadRequestException(`Unique code ${uniqueCode.code} invalid`);
74
69
  }
75
70
  const usages = (_a = uniqueCode.usages) !== null && _a !== void 0 ? _a : [];
76
71
  usages.push(Object.assign(Object.assign({}, metadata), { usedAt: new Date() }));
77
72
  if (uniqueCode.availableUsageCount) {
78
- await uniqueCode.decrement("availableUsageCount");
73
+ await uniqueCode.decrement("availableUsageCount", options);
79
74
  }
80
75
  await this.repository.update({
81
76
  usages
82
- }, {
83
- where: {
77
+ }, Object.assign({ where: {
84
78
  id
85
- }
86
- });
79
+ } }, options));
87
80
  }
88
81
  };
89
82
  SequelizeUniqueCodesService = tslib_1.__decorate([