@tabletennisshop/common 1.0.17 → 1.0.18

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,9 @@
1
+ export declare enum OrderStatusEnum {
2
+ Pending = "pending",// Order placed but not yet processed
3
+ Confirmed = "confirmed",
4
+ Delivering = "delivering",// Order is being shipped
5
+ Finished = "finished",// Order completed/delivered
6
+ Cancelled = "cancelled",// Order canceled by user or system
7
+ Returned = "returned",
8
+ Failed = "failed"
9
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OrderStatusEnum = void 0;
4
+ var OrderStatusEnum;
5
+ (function (OrderStatusEnum) {
6
+ OrderStatusEnum["Pending"] = "pending";
7
+ OrderStatusEnum["Confirmed"] = "confirmed";
8
+ OrderStatusEnum["Delivering"] = "delivering";
9
+ OrderStatusEnum["Finished"] = "finished";
10
+ OrderStatusEnum["Cancelled"] = "cancelled";
11
+ OrderStatusEnum["Returned"] = "returned";
12
+ OrderStatusEnum["Failed"] = "failed";
13
+ })(OrderStatusEnum || (exports.OrderStatusEnum = OrderStatusEnum = {}));
@@ -0,0 +1,4 @@
1
+ export declare enum PaymentMethodEnum {
2
+ Banking = "banking",
3
+ COD = "cod"
4
+ }
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PaymentMethodEnum = void 0;
4
+ var PaymentMethodEnum;
5
+ (function (PaymentMethodEnum) {
6
+ PaymentMethodEnum["Banking"] = "banking";
7
+ PaymentMethodEnum["COD"] = "cod";
8
+ })(PaymentMethodEnum || (exports.PaymentMethodEnum = PaymentMethodEnum = {}));
@@ -0,0 +1,5 @@
1
+ export declare enum UserEnum {
2
+ Client = "client",
3
+ Admin = "admin",
4
+ Employee = "employee"
5
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UserEnum = void 0;
4
+ var UserEnum;
5
+ (function (UserEnum) {
6
+ UserEnum["Client"] = "client";
7
+ UserEnum["Admin"] = "admin";
8
+ UserEnum["Employee"] = "employee";
9
+ })(UserEnum || (exports.UserEnum = UserEnum = {}));
package/build/index.d.ts CHANGED
@@ -6,7 +6,7 @@ export * from './middlewares/check-authorized-middleware';
6
6
  export * from './middlewares/error-handler';
7
7
  export * from './middlewares/validate-request-middleware';
8
8
  export * from './middlewares/current-user-middleware';
9
- export * from './models/client.model';
9
+ export * from './models/user.model';
10
10
  export * from './models/product.model';
11
11
  export * from './models/racket.model';
12
12
  export * from './models/shirt.model';
package/build/index.js CHANGED
@@ -22,7 +22,7 @@ __exportStar(require("./middlewares/check-authorized-middleware"), exports);
22
22
  __exportStar(require("./middlewares/error-handler"), exports);
23
23
  __exportStar(require("./middlewares/validate-request-middleware"), exports);
24
24
  __exportStar(require("./middlewares/current-user-middleware"), exports);
25
- __exportStar(require("./models/client.model"), exports);
25
+ __exportStar(require("./models/user.model"), exports);
26
26
  __exportStar(require("./models/product.model"), exports);
27
27
  __exportStar(require("./models/racket.model"), exports);
28
28
  __exportStar(require("./models/shirt.model"), exports);
@@ -0,0 +1,16 @@
1
+ import { Schema } from "mongoose";
2
+ export interface IAddress {
3
+ province: string;
4
+ district: string;
5
+ ward: string;
6
+ phone_number: string;
7
+ }
8
+ export declare const AddressSchema: Schema<IAddress, import("mongoose").Model<IAddress, any, any, any, import("mongoose").Document<unknown, any, IAddress, any> & IAddress & {
9
+ _id: import("mongoose").Types.ObjectId;
10
+ } & {
11
+ __v: number;
12
+ }, any>, {}, {}, {}, {}, import("mongoose").DefaultSchemaOptions, IAddress, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<IAddress>, {}> & import("mongoose").FlatRecord<IAddress> & {
13
+ _id: import("mongoose").Types.ObjectId;
14
+ } & {
15
+ __v: number;
16
+ }>;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AddressSchema = void 0;
4
+ const mongoose_1 = require("mongoose");
5
+ exports.AddressSchema = new mongoose_1.Schema({
6
+ province: { type: String, required: true },
7
+ district: { type: String, required: true },
8
+ ward: { type: String, required: true },
9
+ phone_number: { type: String, required: true },
10
+ }, { _id: false } //This schema don't need Address Schema
11
+ );
@@ -0,0 +1,24 @@
1
+ import { Document, Model, Types } from "mongoose";
2
+ interface CartAttrs {
3
+ user_id: Types.ObjectId;
4
+ products: [
5
+ {
6
+ product_id: Types.ObjectId;
7
+ quantity: number;
8
+ }
9
+ ];
10
+ }
11
+ interface CartDoc extends Document {
12
+ user_id: Types.ObjectId;
13
+ products: [
14
+ {
15
+ product_id: Types.ObjectId;
16
+ quantity: number;
17
+ }
18
+ ];
19
+ }
20
+ interface CartModel extends Model<CartDoc> {
21
+ build(attrs: CartAttrs): CartDoc;
22
+ }
23
+ export declare const CartModel: CartModel;
24
+ export {};
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CartModel = void 0;
4
+ const mongoose_1 = require("mongoose");
5
+ const CartSchema = new mongoose_1.Schema({
6
+ user_id: { type: mongoose_1.Schema.Types.ObjectId, ref: 'User', required: true },
7
+ products: {
8
+ type: [{
9
+ product_id: { type: mongoose_1.Schema.Types.ObjectId, ref: 'Product', required: true },
10
+ quantity: { type: Number, required: true }
11
+ }],
12
+ required: true
13
+ }
14
+ }, {
15
+ collection: "cart"
16
+ });
17
+ CartSchema.statics.build = (attrs) => {
18
+ return new exports.CartModel(attrs);
19
+ };
20
+ exports.CartModel = (0, mongoose_1.model)('Cart', CartSchema);
@@ -0,0 +1,30 @@
1
+ import { Document, Model, Types } from 'mongoose';
2
+ import { IAddress } from './address.schema';
3
+ import { OrderStatusEnum } from '../enums/order-status.enum';
4
+ import { IStatusTimestamps } from './status-timestamp.schema';
5
+ import { PaymentMethodEnum } from '../enums/payment-method.enum';
6
+ interface IOrderProduct {
7
+ product_id: Types.ObjectId;
8
+ quantity: number;
9
+ }
10
+ export interface OrderAttrs {
11
+ user_id: Types.ObjectId;
12
+ address: IAddress;
13
+ products: IOrderProduct[];
14
+ status: OrderStatusEnum;
15
+ statusTimestamps: IStatusTimestamps;
16
+ payment_method: PaymentMethodEnum;
17
+ }
18
+ export interface OrderDoc extends Document {
19
+ user_id: Types.ObjectId;
20
+ address: IAddress;
21
+ products: IOrderProduct[];
22
+ status?: OrderStatusEnum;
23
+ statusTimestamps: IStatusTimestamps;
24
+ payment_method: PaymentMethodEnum;
25
+ }
26
+ interface OrderModel extends Model<OrderDoc> {
27
+ build(attrs: OrderAttrs): OrderDoc;
28
+ }
29
+ export declare const OrderModel: OrderModel;
30
+ export {};
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OrderModel = void 0;
4
+ const mongoose_1 = require("mongoose");
5
+ const address_schema_1 = require("./address.schema");
6
+ const status_timestamp_schema_1 = require("./status-timestamp.schema");
7
+ const payment_method_enum_1 = require("../enums/payment-method.enum");
8
+ const OrderSchema = new mongoose_1.Schema({
9
+ user_id: { type: mongoose_1.Schema.Types.ObjectId, ref: 'User', required: true }, // FK to User
10
+ address: { type: address_schema_1.AddressSchema, required: true },
11
+ products: [
12
+ {
13
+ product_id: { type: mongoose_1.Schema.Types.ObjectId, ref: 'Product', required: true }, // FK to Product
14
+ quantity: { type: Number, required: true },
15
+ },
16
+ ],
17
+ status: { type: String, required: true },
18
+ statusTimestamps: { type: status_timestamp_schema_1.StatusTimestampsSchema, require: true },
19
+ payment_method: { type: String, enum: payment_method_enum_1.PaymentMethodEnum, required: true }
20
+ }, { collection: 'order' });
21
+ OrderSchema.statics.build = (attrs) => {
22
+ return new exports.OrderModel(attrs);
23
+ };
24
+ exports.OrderModel = (0, mongoose_1.model)('Order', OrderSchema);
@@ -4,7 +4,8 @@ export interface ProductAttrsBase {
4
4
  name: string;
5
5
  brand: string;
6
6
  description: string;
7
- details: any;
7
+ sport: string;
8
+ attributes: any;
8
9
  price: number;
9
10
  }
10
11
  export interface ProductDoc extends Document {
@@ -12,7 +13,9 @@ export interface ProductDoc extends Document {
12
13
  brand: string;
13
14
  description: string;
14
15
  type: ProductEnum;
15
- details: any;
16
+ sport: string;
17
+ attributes: any;
18
+ slug: string;
16
19
  price: number;
17
20
  }
18
21
  export declare const ProductModel: mongoose.Model<ProductDoc, {}, {}, {}, mongoose.Document<unknown, {}, ProductDoc, {}> & ProductDoc & Required<{
@@ -32,30 +32,50 @@ var __importStar = (this && this.__importStar) || (function () {
32
32
  return result;
33
33
  };
34
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
+ };
35
44
  Object.defineProperty(exports, "__esModule", { value: true });
36
45
  exports.ProductModel = void 0;
37
46
  // src/models/product.model.ts
38
47
  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
48
  //We don't need "interface ProductModel extends Model<>" for this
42
49
  const baseOptions = {
43
50
  discriminatorKey: 'type',
44
- collection: 'products',
45
- _id: false,
51
+ collection: 'product',
52
+ timestamps: true
46
53
  };
47
54
  const ProductSchema = new mongoose_1.Schema({
48
- _id: { type: Number },
49
- name: { type: String, required: true },
55
+ name: { type: String, required: true, unique: true },
50
56
  brand: { type: String, required: true },
51
57
  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 },
58
+ type: { type: String, required: true },
59
+ sport: { type: String, required: true },
60
+ attributes: { type: [mongoose_1.Schema.Types.Mixed], require: false },
61
+ slug: { type: String, required: true, unique: true },
54
62
  price: { type: Number, require: true }
55
63
  }, 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" });
64
+ ProductSchema.pre("save", function (next) {
65
+ return __awaiter(this, void 0, void 0, function* () {
66
+ if (this.isModified('name') || !this.slug) {
67
+ this.slug = generateUniqueSlug(this.name);
68
+ }
69
+ next();
70
+ });
71
+ });
61
72
  exports.ProductModel = mongoose_1.default.model('Product', ProductSchema);
73
+ const generateUniqueSlug = (name, currentId) => {
74
+ const baseSlug = name
75
+ .toLowerCase()
76
+ .trim()
77
+ .replace(/[^\w\s-]/g, '') // remove non-word characters
78
+ .replace(/\s+/g, '-') // replace spaces with hyphens
79
+ .replace(/-+/g, '-'); // collapse multiple hyphens
80
+ return baseSlug;
81
+ };
@@ -0,0 +1,19 @@
1
+ import { Schema } from "mongoose";
2
+ export interface IStatusTimestamps {
3
+ pending_at?: Date;
4
+ confirmed_at?: Date;
5
+ delivering_at?: Date;
6
+ finished_at?: Date;
7
+ cancelled_at?: Date;
8
+ returned_at?: Date;
9
+ failed_at?: Date;
10
+ }
11
+ export declare const StatusTimestampsSchema: Schema<IStatusTimestamps, import("mongoose").Model<IStatusTimestamps, any, any, any, import("mongoose").Document<unknown, any, IStatusTimestamps, any> & IStatusTimestamps & {
12
+ _id: import("mongoose").Types.ObjectId;
13
+ } & {
14
+ __v: number;
15
+ }, any>, {}, {}, {}, {}, import("mongoose").DefaultSchemaOptions, IStatusTimestamps, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<IStatusTimestamps>, {}> & import("mongoose").FlatRecord<IStatusTimestamps> & {
16
+ _id: import("mongoose").Types.ObjectId;
17
+ } & {
18
+ __v: number;
19
+ }>;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StatusTimestampsSchema = void 0;
4
+ const mongoose_1 = require("mongoose");
5
+ exports.StatusTimestampsSchema = new mongoose_1.Schema({
6
+ pending_at: { type: Date },
7
+ confirmed_at: { type: Date },
8
+ delivering_at: { type: Date },
9
+ finished_at: { type: Date },
10
+ cancelled_at: { type: Date },
11
+ returned_at: { type: Date },
12
+ failed_at: { type: Date },
13
+ }, { _id: false });
@@ -1,26 +1,30 @@
1
1
  import { Model, Document } from 'mongoose';
2
- export interface ClientAttrs {
2
+ import { IAddress } from './address.schema';
3
+ export interface UserAttrs {
3
4
  email: string;
4
5
  password: string;
5
6
  full_name: string;
6
- address: string;
7
+ addresses: IAddress[];
8
+ type: string;
7
9
  province: string;
8
10
  district: string;
9
11
  ward: string;
10
12
  phone_number: string;
13
+ status: string;
11
14
  }
12
- interface ClientDoc extends Document {
15
+ interface UserDoc extends Document {
13
16
  email: string;
14
17
  password: string;
15
18
  full_name: string;
16
- address: string;
19
+ addresses: IAddress[];
20
+ type: string;
17
21
  province: string;
18
22
  district: string;
19
23
  ward: string;
20
24
  phone_number: string;
21
25
  }
22
- interface ClientModel extends Model<ClientDoc> {
23
- build(attrs: ClientAttrs): ClientDoc;
26
+ interface UserModel extends Model<UserDoc> {
27
+ build(attrs: UserAttrs): UserDoc;
24
28
  }
25
- export declare const ClientModel: ClientModel;
29
+ export declare const UserModel: UserModel;
26
30
  export {};
@@ -0,0 +1,46 @@
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.UserModel = void 0;
13
+ const mongoose_1 = require("mongoose");
14
+ const password_1 = require("../services/password");
15
+ const user_enum_1 = require("../enums/user.enum");
16
+ const address_schema_1 = require("./address.schema");
17
+ const UserSchema = new mongoose_1.Schema({
18
+ email: { type: String, required: true, unique: true },
19
+ password: { type: String, required: true, minlength: 4 },
20
+ full_name: { type: String, required: true },
21
+ addresses: [
22
+ { type: address_schema_1.AddressSchema, required: true }
23
+ ],
24
+ type: { type: String, enum: Object.values(user_enum_1.UserEnum), required: true },
25
+ }, {
26
+ timestamps: true, // Adds createdAt and updatedAt fields automatically
27
+ collection: "user"
28
+ });
29
+ UserSchema.statics.build = (attrs) => {
30
+ return new exports.UserModel(attrs);
31
+ };
32
+ UserSchema.pre('save', function (next) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ if (this.isModified('email')) {
35
+ const existingUser = yield exports.UserModel.findOne({ email: this.email });
36
+ if (existingUser) {
37
+ throw new Error("Email already exists!");
38
+ }
39
+ }
40
+ if (this.isModified('password')) {
41
+ this.password = yield password_1.Password.toHash(this.password);
42
+ }
43
+ next();
44
+ });
45
+ });
46
+ exports.UserModel = (0, mongoose_1.model)('User', UserSchema);
@@ -0,0 +1,18 @@
1
+ import { Types, Document } from 'mongoose';
2
+ interface IVendorPurchase {
3
+ vendor_id: Types.ObjectId;
4
+ products: {
5
+ product_id: Types.ObjectId;
6
+ price: number;
7
+ quantity: number;
8
+ }[];
9
+ date: Date;
10
+ }
11
+ interface VendorPurchaseDoc extends IVendorPurchase, Document {
12
+ }
13
+ export declare const VendorPurchaseModel: import("mongoose").Model<VendorPurchaseDoc, {}, {}, {}, Document<unknown, {}, VendorPurchaseDoc, {}> & VendorPurchaseDoc & Required<{
14
+ _id: unknown;
15
+ }> & {
16
+ __v: number;
17
+ }, any>;
18
+ export {};
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VendorPurchaseModel = void 0;
4
+ const mongoose_1 = require("mongoose");
5
+ const VendorPurchaseSchema = new mongoose_1.Schema({
6
+ vendor_id: { type: mongoose_1.Schema.Types.ObjectId, ref: 'Vendor', required: true },
7
+ products: [
8
+ {
9
+ product_id: { type: mongoose_1.Schema.Types.ObjectId, ref: 'Product', required: true },
10
+ price: { type: Number, required: true },
11
+ quantity: { type: Number, required: true },
12
+ },
13
+ ],
14
+ date: { type: Date, required: true },
15
+ }, {
16
+ collection: 'vendor_purchase',
17
+ timestamps: true,
18
+ });
19
+ exports.VendorPurchaseModel = (0, mongoose_1.model)('VendorPurchase', VendorPurchaseSchema);
@@ -0,0 +1,15 @@
1
+ import { Document, Model } from "mongoose";
2
+ import { IAddress } from "./address.schema";
3
+ interface VendorAttrs {
4
+ name: string;
5
+ address: IAddress;
6
+ }
7
+ interface VendorDoc extends Document {
8
+ name: string;
9
+ address: IAddress;
10
+ }
11
+ interface VendorModel extends Model<VendorDoc> {
12
+ build(attr: VendorAttrs): VendorDoc;
13
+ }
14
+ export declare const VendorModel: VendorModel;
15
+ export {};
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VendorModel = void 0;
4
+ const mongoose_1 = require("mongoose");
5
+ const address_schema_1 = require("./address.schema");
6
+ const VendorSchema = new mongoose_1.Schema({
7
+ name: { type: String, required: true },
8
+ address: { type: address_schema_1.AddressSchema, required: true }
9
+ }, {
10
+ timestamps: true,
11
+ collection: "vendor"
12
+ });
13
+ VendorSchema.statics.build = (attrs) => {
14
+ return new exports.VendorModel(attrs);
15
+ };
16
+ exports.VendorModel = (0, mongoose_1.model)('Vendor', VendorSchema);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tabletennisshop/common",
3
- "version": "1.0.17",
3
+ "version": "1.0.18",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -1,81 +0,0 @@
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);