-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct.js
159 lines (149 loc) · 4.03 KB
/
product.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const { Schema, model } = require('mongoose');
const Joi = require('joi');
const { handleMongooseError, validationRules } = require('../helpers');
// Define Joi validation rules for model type
const validationAddModelType = Joi.object({
product: Joi.string()
.description('Product type name')
.min(2)
.max(50)
.required()
.messages({
...validationRules('Product type name').textRules,
...validationRules('Product type name').commonRules,
}),
code: Joi.string()
.description('Code of model type')
.min(1)
.max(50)
.required()
.messages({
...validationRules('Code of model type').textRules,
...validationRules('Code of model type').commonRules,
}),
price: Joi.number()
.description('Price')
.min(1)
.max(100000)
.messages(validationRules('Price').numberRules),
sizes: Joi.array().items(
Joi.number()
.description('Size of model type')
.min(10)
.max(99)
.messages(validationRules('Size of model type').numberRules),
),
categories: Joi.array().items(
Joi.string()
.description('Category of product')
.length(24)
.messages(validationRules('Category ID').textRules),
),
subcategories: Joi.array().items(
Joi.string()
.description('Subcategory of product')
.length(24)
.messages(validationRules('Subcategory ID').textRules),
),
});
const validationUpdateModelType = Joi.object({
product: Joi.string()
.description('Product type name')
.min(2)
.max(50)
.messages(validationRules('Product type name').textRules),
price: Joi.number()
.description('Price')
.min(1)
.max(100000)
.messages(validationRules('Price').numberRules),
categories: Joi.array().items(
Joi.string()
.description('Category of product')
.length(24)
.messages(validationRules('Category ID').textRules),
),
subcategories: Joi.array().items(
Joi.string()
.description('Subcategory of product')
.length(24)
.messages(validationRules('Subcategory ID').textRules),
),
});
// Define Joi validation rules for model
const validationAddModel = Joi.object({
productModel: Joi.string()
.description('Product model name')
.min(2)
.max(50)
.required()
.messages({
...validationRules('Product model name').textRules,
...validationRules('Product model name').commonRules,
}),
brand: Joi.string()
.description('Brand name')
.min(2)
.max(50)
.messages(validationRules('Brand name').textRules),
productModelTypes: Joi.array().items(validationAddModelType),
});
const validationUpdateModel = Joi.object({
productModel: Joi.string()
.description('Product model name')
.min(2)
.max(50)
.messages(validationRules('Product model name').textRules),
brand: Joi.string()
.description('Brand name')
.min(2)
.max(50)
.messages(validationRules('Brand name').textRules),
});
// Define Mongoose schema for the model
const productSchema = new Schema(
{
product: {
type: String,
required: true,
},
code: {
type: String,
required: true,
},
price: {
type: Number,
default: 0,
},
sizes: [Number],
categories: [{ _id: Schema.Types.ObjectId, categoryName: String }],
subcategories: [{ _id: Schema.Types.ObjectId, subcategoryName: String }],
},
{ versionKey: false, timestamps: true },
);
// Define Mongoose schema for the brand, which includes an array of models
const productModelSchema = new Schema(
{
productModel: {
type: String,
required: true,
},
productModelTypes: [productSchema],
brand: {
type: String,
default: '',
},
},
{ versionKey: false, timestamps: true },
);
// Attach a post-save middleware to handle Mongoose errors
productModelSchema.post('save', handleMongooseError);
// Create a Mongoose model
const Product = model('product', productModelSchema);
module.exports = {
Product,
validationAddModel,
validationUpdateModel,
validationAddModelType,
validationUpdateModelType,
};