@common_ch/common 1.0.111 → 1.0.113

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.
@@ -6,6 +6,6 @@ export interface PlanBuyEvent {
6
6
  userId: string;
7
7
  price: string;
8
8
  versionPlan: number;
9
- versionUser: number;
9
+ versionUserPlanMessage: number;
10
10
  };
11
11
  }
@@ -10,6 +10,7 @@ export interface UserAttrs {
10
10
  planName?: string;
11
11
  startTimePlan?: string;
12
12
  exTimePlan?: string;
13
+ versionPlanMessage?: string;
13
14
  amountApiExistPlan?: number;
14
15
  amountUploadExistPlan?: number;
15
16
  email?: string;
@@ -37,6 +38,7 @@ export interface UserDoc extends mongoose.Document {
37
38
  planName?: string;
38
39
  startTimePlan?: string;
39
40
  exTimePlan: string;
41
+ versionPlanMessage: string;
40
42
  amountApiExistPlan: number;
41
43
  amountUploadExistPlan: number;
42
44
  email?: string;
@@ -14,7 +14,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.User = exports.userStatusEnum = void 0;
16
16
  const mongoose_1 = __importDefault(require("mongoose"));
17
- const mongoose_update_if_current_1 = require("mongoose-update-if-current");
17
+ // import { updateIfCurrentPlugin } from 'mongoose-update-if-current';
18
+ const password_1 = require("../../services/password");
18
19
  var userStatusEnum;
19
20
  (function (userStatusEnum) {
20
21
  userStatusEnum["active"] = "active";
@@ -38,6 +39,11 @@ const userSchema = new mongoose_1.default.Schema({
38
39
  ref: 'Plan',
39
40
  required: false,
40
41
  },
42
+ versionPlanMessage: {
43
+ type: Number,
44
+ required: true,
45
+ default: 0,
46
+ },
41
47
  username: {
42
48
  type: String,
43
49
  required: false,
@@ -123,10 +129,31 @@ const userSchema = new mongoose_1.default.Schema({
123
129
  },
124
130
  });
125
131
  userSchema.set('versionKey', 'version');
126
- userSchema.plugin(mongoose_update_if_current_1.updateIfCurrentPlugin);
132
+ // userSchema.plugin(updateIfCurrentPlugin);
127
133
  userSchema.statics.build = (attrs) => {
128
134
  return new User(attrs);
129
135
  };
136
+ userSchema.pre('save', function (done) {
137
+ return __awaiter(this, void 0, void 0, function* () {
138
+ if (this.isModified('password')) {
139
+ if (this.get('password')) {
140
+ const hashed = yield password_1.Password.toHash(this.get('password'));
141
+ this.set('password', hashed);
142
+ }
143
+ }
144
+ if (this.isModified('versionPlanMessage')) {
145
+ this.$where = {
146
+ versionPlanMessage: this.get('versionPlanMessage'),
147
+ };
148
+ }
149
+ else {
150
+ this.$where = {
151
+ version: this.get('version'),
152
+ };
153
+ }
154
+ done();
155
+ });
156
+ });
130
157
  userSchema.methods.updateWalletBalance = function (amount) {
131
158
  return __awaiter(this, void 0, void 0, function* () {
132
159
  const currentBalance = parseFloat(this.walletBalance.toString());
@@ -0,0 +1,4 @@
1
+ export declare class Password {
2
+ static toHash(password: string): Promise<string>;
3
+ static compare(storedPassword: string, suppliedPassword: string): Promise<boolean>;
4
+ }
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Password = void 0;
13
+ const crypto_1 = require("crypto");
14
+ const util_1 = require("util");
15
+ const scryptAsync = (0, util_1.promisify)(crypto_1.scrypt);
16
+ class Password {
17
+ static toHash(password) {
18
+ return __awaiter(this, void 0, void 0, function* () {
19
+ const salt = (0, crypto_1.randomBytes)(8).toString('hex');
20
+ const buf = (yield scryptAsync(password, salt, 64));
21
+ return `${buf.toString('hex')}.${salt}`;
22
+ });
23
+ }
24
+ static compare(storedPassword, suppliedPassword) {
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ const [hashedPassword, salt] = storedPassword.split('.');
27
+ const buf = (yield scryptAsync(suppliedPassword, salt, 64));
28
+ return buf.toString('hex') === hashedPassword;
29
+ });
30
+ }
31
+ }
32
+ exports.Password = Password;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@common_ch/common",
3
- "version": "1.0.111",
3
+ "version": "1.0.113",
4
4
  "main": "./build/index.js",
5
5
  "types": "./build/index.d.ts",
6
6
  "files": [
@@ -25,6 +25,7 @@
25
25
  "@types/express": "^5.0.0",
26
26
  "@types/jsonwebtoken": "^9.0.7",
27
27
  "cookie-session": "^2.1.0",
28
+ "crypto": "^1.0.1",
28
29
  "express": "^4.21.2",
29
30
  "joi": "^17.13.3",
30
31
  "jsonwebtoken": "^9.0.2",