Skip to content

Commit 2744d14

Browse files
committed
product service
1 parent 5cdff90 commit 2744d14

File tree

9 files changed

+216
-113
lines changed

9 files changed

+216
-113
lines changed

.env

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# NODE_ENV=production
2-
NODE_ENV=development
1+
NODE_ENV=production
2+
# NODE_ENV=development
3+
34

45
# General Configuration
56
PORT=4010

controller/product.controller.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module.exports = {
3232
return res.send({statusCode:successCode, data : {}, message: 'No Product found from this Product Id'});
3333
}
3434
} catch (error) {
35-
console.log("PRODUCT CONTROLLER -- addProduct :: ", error);
35+
console.log("PRODUCT CONTROLLER -- getProductById :: ", error);
3636
return handleCustomError(res, error)
3737
}
3838
},
@@ -46,7 +46,7 @@ module.exports = {
4646
}
4747
return res.send({statusCode:successCode, data:response, message: 'All Product fetched successfully'});
4848
} catch (error) {
49-
console.log("PRODUCT CONTROLLER -- addProduct :: ", error);
49+
console.log("PRODUCT CONTROLLER -- getProducts :: ", error);
5050
return handleCustomError(res, error)
5151
}
5252
}

models/contact.model.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

models/order.model.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const mongoose = require("mongoose");
2+
3+
const OrderSchema = new mongoose.Schema({
4+
orderId: {
5+
type: String,
6+
required: true,
7+
},
8+
productId: {
9+
type: mongoose.Schema.Types.ObjectId, ref: "Product",
10+
required: true,
11+
},
12+
variantId: {
13+
type: mongoose.Schema.Types.ObjectId, ref: "Variant",
14+
required: true,
15+
},
16+
userId: {
17+
type: mongoose.Schema.Types.ObjectId, ref: "User",
18+
required: true,
19+
},
20+
quantity: {
21+
type: Number,
22+
required: true,
23+
},
24+
price: {
25+
type: Number,
26+
required: true,
27+
},
28+
discount: {
29+
type: Number,
30+
required: true,
31+
},
32+
discountType: {
33+
type: String,
34+
required: true,
35+
},
36+
totalDiscount: {
37+
type: String,
38+
required: true,
39+
},
40+
totalPrice: {
41+
type: String,
42+
required: true,
43+
},
44+
paymentMode: {
45+
type: String,
46+
required: true,
47+
},
48+
paymentStatus: {
49+
type: String,
50+
required: true,
51+
},
52+
deliveryAddress: {
53+
type: String,
54+
required: true,
55+
},
56+
deliveryStatus: {
57+
type: String,
58+
required: true,
59+
},
60+
deliveryDate: {
61+
type: String,
62+
required: true,
63+
},
64+
deliveryTime: {
65+
type: String,
66+
required: true,
67+
},
68+
date: { type: Date, default: Date.now },
69+
});
70+
71+
const Order = mongoose.model("Order", OrderSchema);
72+
73+
module.exports = Order;

models/product.model.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const ProductSchema = new mongoose.Schema({
1414
required : true
1515
},
1616
categoryId : {
17-
type : Number,
17+
type : mongoose.Schema.Types.ObjectId, ref:'Category',
1818
required : true
1919
},
2020
image1 : {
@@ -27,8 +27,8 @@ const ProductSchema = new mongoose.Schema({
2727
image3 : {
2828
type : String
2929
},
30-
date: { type: Date, default: Date.now },
31-
})
30+
date: { type: Date, default: Date.now }
31+
}, {timestamps:true, timeseries:true})
3232

3333
const Product = mongoose.model("Product", ProductSchema)
3434

models/productstock.model.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const mongoose = require('mongoose');
22

33
const ProductStockSchema = new mongoose.Schema({
4-
productid : {
5-
type : String,
4+
productId : {
5+
type : mongoose.Schema.Types.ObjectId, ref:'Product',
66
required : true
77
},
8-
variantid : {
9-
type : String,
8+
variantId : {
9+
type : mongoose.Schema.Types.ObjectId, ref:'Variant',
1010
required : true
1111
},
1212
quatity : {
@@ -22,7 +22,7 @@ const ProductStockSchema = new mongoose.Schema({
2222
required : true
2323
},
2424
date: { type: Date, default: Date.now },
25-
})
25+
}, {timestamps:true, timeseries:true})
2626

2727
const ProductStock = mongoose.model("productstock", ProductStockSchema)
2828

models/user.model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const userSchema = new mongoose.Schema({
4141
type: String,
4242
},
4343
date: { type: Date, default: Date.now },
44-
})
44+
}, {timestamps:true, timeseries:true})
4545

4646
const User = mongoose.model("User", userSchema)
4747

services/product.service.js

Lines changed: 94 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,97 @@
1-
const Product = require("../models/product.model")
2-
const Variant = require("../models/variant.model")
1+
const Product = require("../models/product.model");
2+
const Variant = require("../models/variant.model");
3+
const mongoose = require("mongoose");
34

45
module.exports = {
5-
createNewProduct : async(item) => {
6-
try {
7-
let newEntry = new Product(item)
8-
const response = await newEntry.save()
9-
return { status:1, data:response }
10-
} catch (error) {
11-
console.error('Error in PRODUCT SERVICE :: createNewProduct : ', error);
12-
throw error;
13-
}
14-
},
15-
createNewVariants : async(item) => {
16-
try {
17-
const response = await Variant.insertMany(item)
18-
return { status:1, data:response }
19-
} catch (error) {
20-
console.error('Error in PRODUCT SERVICE :: createNewVariants : ', error);
21-
throw error;
22-
}
23-
},
24-
getProductById : async(_id) => {
25-
try {
26-
const response = Product.findById({_id}, {__v:0})
27-
return response
28-
} catch (error) {
29-
console.error('Error in PRODUCT SERVICE :: getProductById : ', error);
30-
throw error;
31-
}
32-
},
33-
getProducts : async(skip, limit) => {
34-
try {
35-
const response = Product.find({}, {productId:1, title:1, categoryId:1, image1:1}, { skip: skip, limit: limit })
36-
.sort({ _id:-1})
37-
return response
38-
} catch (error) {
39-
console.error('Error in PRODUCT SERVICE :: getProducts : ', error);
40-
throw error;
41-
}
6+
createNewProduct: async (item) => {
7+
try {
8+
let newEntry = new Product(item);
9+
const response = await newEntry.save();
10+
return { status: 1, data: response };
11+
} catch (error) {
12+
console.error("Error in PRODUCT SERVICE :: createNewProduct : ", error);
13+
throw error;
4214
}
43-
}
15+
},
16+
createNewVariants: async (item) => {
17+
try {
18+
const response = await Variant.insertMany(item);
19+
return { status: 1, data: response };
20+
} catch (error) {
21+
console.error("Error in PRODUCT SERVICE :: createNewVariants : ", error);
22+
throw error;
23+
}
24+
},
25+
getProductById: async (_id) => {
26+
try {
27+
// const response = Product.findById({_id}, {__v:0}).populate('categoryId')
28+
const response = Product.aggregate([
29+
// Match product by ID
30+
// IF IT WORKS DO NOT TOUCH
31+
{ $match: { _id: new mongoose.Types.ObjectId(_id) } },
32+
// Join with category table
33+
{
34+
$lookup: {
35+
from: "categories",
36+
localField: "categoryId",
37+
foreignField: "_id",
38+
as: "category",
39+
},
40+
},
41+
// Unwind category array (this unwind is used to convert single array to an object)
42+
{ $unwind: "$category" },
43+
// Lookup variants for the product (added variant table with parent table product)
44+
{
45+
$lookup: {
46+
from: "variants",
47+
localField: "_id",
48+
foreignField: "productId",
49+
as: "variant",
50+
},
51+
},
52+
// project is used to filter the data which i want from all the table
53+
{
54+
$project: {
55+
_id: 1,
56+
productId: 1,
57+
title: 1,
58+
description: 1,
59+
categoryId: 1,
60+
image1: 1,
61+
category: {
62+
_id: 1,
63+
title: 1,
64+
image1: 1,
65+
},
66+
variant: {
67+
variantId: 1,
68+
variantName: 1,
69+
price: 1,
70+
discount: 1,
71+
discountType: 1,
72+
},
73+
},
74+
},
75+
]);
76+
return response;
77+
} catch (error) {
78+
console.error("Error in PRODUCT SERVICE :: getProductById : ", error);
79+
throw error;
80+
}
81+
},
82+
getProducts: async (skip, limit) => {
83+
try {
84+
const response = Product.find(
85+
{},
86+
{ productId: 1, title: 1, categoryId: 1, image1: 1 }
87+
)
88+
.skip(skip)
89+
.limit(limit)
90+
.sort({ _id: -1 });
91+
return response;
92+
} catch (error) {
93+
console.error("Error in PRODUCT SERVICE :: getProducts : ", error);
94+
throw error;
95+
}
96+
},
97+
};

utils/express.js

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
1-
const express = require('express');
2-
const cors = require('cors');
3-
const config = require('./config');
1+
const express = require("express");
2+
const cors = require("cors");
3+
const config = require("./config");
44
module.exports = (app) => {
5-
app.use(cors({
6-
origin:[
7-
'http://localhost:3000'
8-
]
9-
}));
10-
app.use(express.json({ limit: '500mb' }));
11-
app.use(express.urlencoded({ limit: '500mb', extended: true }));
12-
// app.use(express.static('./public'));
13-
app.all('/', (req, res, next) => {
14-
// CORS headers
15-
res.header('Access-Control-Allow-Origin', '*');
16-
res.header('Access-Control-Allow-Methods', 'GET,POST');
17-
// Setting custom headers for CORS
18-
res.header('Access-Control-Allow-Headers', 'Content-Type,Accept,X-Access-Token,X-Key,Authorization,Client-Key');
19-
if (req.method === 'OPTIONS') {
20-
res.status(200).end();
21-
} else {
22-
next();
23-
}
24-
});
25-
// simple route
26-
app.get('/', (req, res) => {
27-
res.json({message: `Welcome to ${config.APP_NAME} Api`});
28-
});
29-
30-
app.use('/api',require('../routes'))
31-
32-
return app;
33-
};
5+
app.use(
6+
cors({
7+
origin: ["http://localhost:3000"],
8+
})
9+
);
10+
app.use(express.json({ limit: "500mb" }));
11+
app.use(express.urlencoded({ limit: "500mb", extended: true }));
12+
// app.use(express.static('./public'));
13+
app.all("/", (req, res, next) => {
14+
// CORS headers
15+
res.header("Access-Control-Allow-Origin", "*");
16+
res.header("Access-Control-Allow-Methods", "GET,POST");
17+
// Setting custom headers for CORS
18+
res.header(
19+
"Access-Control-Allow-Headers",
20+
"Content-Type,Accept,X-Access-Token,X-Key,Authorization,Client-Key"
21+
);
22+
if (req.method === "OPTIONS") {
23+
res.status(200).end();
24+
} else {
25+
next();
26+
}
27+
});
28+
// simple route
29+
app.get("/", (req, res) => {
30+
res.json({ message: `Welcome to ${config.APP_NAME} Api` });
31+
});
32+
33+
app.use("/api", require("../routes"));
34+
35+
return app;
36+
};

0 commit comments

Comments
 (0)