@tabletennisshop/common 1.0.13 → 1.0.15

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.
@@ -0,0 +1,5 @@
1
+ export declare enum ProductEnum {
2
+ Racket = "racket",
3
+ Shirt = "shirt",
4
+ Sponge = "sponge"
5
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ProductEnum = void 0;
4
+ var ProductEnum;
5
+ (function (ProductEnum) {
6
+ ProductEnum["Racket"] = "racket";
7
+ ProductEnum["Shirt"] = "shirt";
8
+ ProductEnum["Sponge"] = "sponge";
9
+ })(ProductEnum || (exports.ProductEnum = ProductEnum = {}));
@@ -18,7 +18,6 @@ const CurrentUserMiddleware = (req, res, next) => {
18
18
  try {
19
19
  const payload = jsonwebtoken_1.default.verify((_b = req.session) === null || _b === void 0 ? void 0 : _b.jwt, "secretkey");
20
20
  req.currentUser = payload;
21
- console.log("here1");
22
21
  }
23
22
  catch (err) {
24
23
  console.log(err);
@@ -0,0 +1,26 @@
1
+ import { Model, Document } from 'mongoose';
2
+ export interface ClientAttrs {
3
+ email: string;
4
+ password: string;
5
+ full_name: string;
6
+ address: string;
7
+ province: string;
8
+ district: string;
9
+ ward: string;
10
+ phone_number: string;
11
+ }
12
+ interface ClientDoc extends Document {
13
+ email: string;
14
+ password: string;
15
+ full_name: string;
16
+ address: string;
17
+ province: string;
18
+ district: string;
19
+ ward: string;
20
+ phone_number: string;
21
+ }
22
+ interface ClientModel extends Model<ClientDoc> {
23
+ build(attrs: ClientAttrs): ClientDoc;
24
+ }
25
+ export declare const ClientModel: ClientModel;
26
+ export {};
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
+ return new (P || (P = Promise))(function (resolve, reject) {
38
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
39
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
40
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
41
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
42
+ });
43
+ };
44
+ Object.defineProperty(exports, "__esModule", { value: true });
45
+ exports.ClientModel = void 0;
46
+ const mongoose_1 = __importStar(require("mongoose"));
47
+ const password_1 = require("../services/password");
48
+ const AutoIncrement = require('mongoose-sequence')(mongoose_1.default);
49
+ const ClientSchema = new mongoose_1.Schema({
50
+ _id: { type: Number },
51
+ email: { type: String, required: true, unique: true },
52
+ password: { type: String, required: true, minlength: 4 },
53
+ full_name: { type: String, required: true },
54
+ address: { type: String, required: true },
55
+ province: { type: String, required: true },
56
+ district: { type: String, required: true },
57
+ ward: { type: String, required: true },
58
+ phone_number: { type: String, required: true }
59
+ }, {
60
+ timestamps: true, // Adds createdAt and updatedAt fields automatically
61
+ _id: false
62
+ });
63
+ ClientSchema.plugin(AutoIncrement, { inc_field: '_id', id: "client_id_seq" });
64
+ ClientSchema.statics.build = (attrs) => {
65
+ return new exports.ClientModel(attrs);
66
+ };
67
+ ClientSchema.pre('save', function (next) {
68
+ return __awaiter(this, void 0, void 0, function* () {
69
+ if (this.isModified('email')) {
70
+ const existingUser = yield exports.ClientModel.findOne({ email: this.email });
71
+ if (existingUser) {
72
+ throw new Error("Email already exists!");
73
+ }
74
+ }
75
+ if (this.isModified('password')) {
76
+ this.password = yield password_1.Password.toHash(this.password);
77
+ }
78
+ next();
79
+ });
80
+ });
81
+ exports.ClientModel = (0, mongoose_1.model)('Client', ClientSchema);
@@ -0,0 +1,22 @@
1
+ import mongoose, { Document } from 'mongoose';
2
+ import { ProductEnum } from '../enums/product.enum';
3
+ export interface ProductAttrsBase {
4
+ name: string;
5
+ brand: string;
6
+ description: string;
7
+ details: any;
8
+ price: number;
9
+ }
10
+ export interface ProductDoc extends Document {
11
+ name: string;
12
+ brand: string;
13
+ description: string;
14
+ type: ProductEnum;
15
+ details: any;
16
+ price: number;
17
+ }
18
+ export declare const ProductModel: mongoose.Model<ProductDoc, {}, {}, {}, mongoose.Document<unknown, {}, ProductDoc, {}> & ProductDoc & Required<{
19
+ _id: unknown;
20
+ }> & {
21
+ __v: number;
22
+ }, any>;
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.ProductModel = void 0;
37
+ // src/models/product.model.ts
38
+ const mongoose_1 = __importStar(require("mongoose"));
39
+ const product_enum_1 = require("../enums/product.enum");
40
+ const AutoIncrement = require('mongoose-sequence')(mongoose_1.default);
41
+ //We don't need "interface ProductModel extends Model<>" for this
42
+ const baseOptions = {
43
+ discriminatorKey: 'type',
44
+ collection: 'products',
45
+ _id: false,
46
+ };
47
+ const ProductSchema = new mongoose_1.Schema({
48
+ _id: { type: Number },
49
+ name: { type: String, required: true },
50
+ brand: { type: String, required: true },
51
+ description: { type: String, required: false },
52
+ type: { type: String, enum: Object.values(product_enum_1.ProductEnum), required: true },
53
+ details: { type: [mongoose_1.Schema.Types.Mixed], require: false },
54
+ price: { type: Number, require: true }
55
+ }, baseOptions);
56
+ /*
57
+ inc_field is the field in the Collections
58
+ id: This must be unique in each Incremental Fields
59
+ */
60
+ ProductSchema.plugin(AutoIncrement, { inc_field: '_id', id: "product_id_seq" });
61
+ exports.ProductModel = mongoose_1.default.model('Product', ProductSchema);
@@ -0,0 +1,14 @@
1
+ import { Model } from "mongoose";
2
+ import { ProductEnum } from "../enums/product.enum";
3
+ import { ProductAttrsBase, ProductDoc } from "./product.model";
4
+ export interface RacketAttrs extends ProductAttrsBase {
5
+ type: ProductEnum.Racket;
6
+ }
7
+ export interface RacketDoc extends ProductDoc {
8
+ type: ProductEnum.Racket;
9
+ }
10
+ interface RacketModel extends Model<RacketDoc> {
11
+ build(attrs: RacketAttrs): RacketDoc;
12
+ }
13
+ export declare const RacketModel: RacketModel;
14
+ export {};
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RacketModel = void 0;
4
+ const mongoose_1 = require("mongoose");
5
+ const product_enum_1 = require("../enums/product.enum");
6
+ const product_model_1 = require("./product.model");
7
+ const racketSchema = new mongoose_1.Schema({});
8
+ racketSchema.statics.build = (attrs) => {
9
+ return new exports.RacketModel(attrs);
10
+ };
11
+ exports.RacketModel = product_model_1.ProductModel.discriminator(product_enum_1.ProductEnum.Racket, racketSchema);
@@ -0,0 +1,8 @@
1
+ import { ProductEnum } from "../enums/product.enum";
2
+ import { ProductAttrsBase, ProductDoc } from "./product.model";
3
+ export interface ShirtAttrs extends ProductAttrsBase {
4
+ type: ProductEnum.Shirt;
5
+ }
6
+ export interface ShirtDoc extends ProductDoc {
7
+ type: ProductEnum.Shirt;
8
+ }
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const mongoose_1 = require("mongoose");
4
+ const product_enum_1 = require("../enums/product.enum");
5
+ const product_model_1 = require("./product.model");
6
+ const shirtSchema = new mongoose_1.Schema({});
7
+ shirtSchema.statics.build = (attrs) => {
8
+ return new ShirtModel(attrs);
9
+ };
10
+ const ShirtModel = product_model_1.ProductModel.discriminator(product_enum_1.ProductEnum.Shirt, new mongoose_1.Schema({}));
@@ -0,0 +1,8 @@
1
+ import { ProductEnum } from "../enums/product.enum";
2
+ import { ProductAttrsBase, ProductDoc } from "./product.model";
3
+ export interface SpongeAttrs extends ProductAttrsBase {
4
+ type: ProductEnum.Sponge;
5
+ }
6
+ export interface SpongeDoc extends ProductDoc {
7
+ type: ProductEnum.Sponge;
8
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const mongoose_1 = require("mongoose");
4
+ const product_enum_1 = require("../enums/product.enum");
5
+ const product_model_1 = require("./product.model");
6
+ const spongeSchema = new mongoose_1.Schema({});
7
+ spongeSchema.statics.build = (attrs) => {
8
+ return new SpongeModel(attrs);
9
+ };
10
+ // ─────────── Sponge ───────────
11
+ const SpongeModel = product_model_1.ProductModel.discriminator(product_enum_1.ProductEnum.Sponge, new mongoose_1.Schema({}));
@@ -0,0 +1,4 @@
1
+ export declare class Password {
2
+ static toHash(password: string): Promise<string>;
3
+ static compare(input_password: string, stored_password: string): Promise<boolean>;
4
+ }
@@ -0,0 +1,30 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.Password = void 0;
16
+ const bcrypt_1 = __importDefault(require("bcrypt"));
17
+ class Password {
18
+ static toHash(password) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ const saltRounds = 10;
21
+ return bcrypt_1.default.hash(password, saltRounds);
22
+ });
23
+ }
24
+ static compare(input_password, stored_password) {
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ return bcrypt_1.default.compare(input_password, stored_password);
27
+ });
28
+ }
29
+ }
30
+ exports.Password = Password;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tabletennisshop/common",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -18,14 +18,17 @@
18
18
  "type": "commonjs",
19
19
  "dependencies": {
20
20
  "@tabletennisshop/common": "^1.0.1",
21
+ "@types/bcrypt": "^5.0.2",
21
22
  "@types/cookie-session": "^2.0.49",
22
23
  "@types/express": "^5.0.2",
23
24
  "@types/jsonwebtoken": "^9.0.9",
24
25
  "axios": "^1.9.0",
26
+ "bcrypt": "^6.0.0",
25
27
  "cookie-session": "^2.1.0",
26
28
  "express": "^5.1.0",
27
29
  "express-validator": "^7.2.1",
28
30
  "jsonwebtoken": "^9.0.2",
31
+ "mongoose": "^8.15.1",
29
32
  "uninstall": "^0.0.0"
30
33
  },
31
34
  "devDependencies": {