-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
166 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { asyncWrapper } from '../../../utils/wrapper'; | ||
import { ProductService } from './product.service'; | ||
|
||
export class ProductController { | ||
private service: ProductService; | ||
|
||
constructor() { | ||
this.service = new ProductService(); | ||
} | ||
|
||
addnewProduct = asyncWrapper(async (req, res) => { | ||
await this.service.addNewProduct(req.body); | ||
return res.json({ message: 'Product created successfully' }); | ||
}); | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Schema, model } from 'mongoose'; | ||
|
||
// Define the schema for the Product | ||
const productSchema = new Schema( | ||
{ | ||
user: { | ||
type: Schema.Types.ObjectId, | ||
ref: 'User', // Reference to the User model | ||
required: true, | ||
}, | ||
batchNo: { | ||
type: String, | ||
required: true, | ||
}, | ||
qty: { | ||
type: Number, | ||
min: 0, | ||
default: 0, | ||
}, | ||
reorderQty: { | ||
type: Number, | ||
min: 0, | ||
default: 0, | ||
}, | ||
salesPrice: { | ||
type: Number, | ||
required: true, | ||
min: 0, | ||
}, | ||
costPrice: { | ||
type: Number, | ||
required: true, | ||
min: 0, | ||
}, | ||
}, | ||
{ | ||
timestamps: true, | ||
}, | ||
); | ||
|
||
// Create the Product model | ||
const ProductModel = model('Product', productSchema); | ||
|
||
export default ProductModel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Router } from 'express'; | ||
import { ProductController } from './product.controller'; | ||
|
||
class ProductRouter { | ||
public router: Router; | ||
|
||
private controller: ProductController; | ||
|
||
constructor() { | ||
this.router = Router(); | ||
this.controller = new ProductController(); | ||
this.mountRoutes(); | ||
} | ||
|
||
mountRoutes() { | ||
this.router.post('/', this.controller.addnewProduct); | ||
} | ||
} | ||
|
||
const productRoutes = new ProductRouter().router; | ||
export default productRoutes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import ProductModel from './product.model'; | ||
import { productSchema, TProductSchema } from '../../../schema/product.schema'; | ||
import { BadRequestError } from '../../../utils/exceptions'; | ||
export class ProductService { | ||
async addNewProduct(payload: TProductSchema) { | ||
const { data, success } = productSchema.safeParse(payload); | ||
if (!success) throw new BadRequestError('Invalid payload format'); | ||
const newProduct = new ProductModel(data); | ||
await newProduct.save(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import jwt from 'jsonwebtoken'; | ||
import { ForbiddenError, UnauthorizedError } from './utils/exceptions'; | ||
import UserModel from './app/user/user.model'; | ||
|
||
export const isAuthencticated = ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
) => { | ||
let token = req.headers.authorization; | ||
if (!token) { | ||
throw new UnauthorizedError('Token is required'); | ||
} | ||
if (token.startsWith('Bearer ')) { | ||
token = token.slice(7, token.length).trim(); | ||
} | ||
|
||
try { | ||
const decoded = jwt.verify(token, process.env.JWT_SECRET); | ||
console.log('decoded', decoded); | ||
req.params.userId = (decoded as any).userId; | ||
next(); | ||
} catch (err) { | ||
console.error(err); | ||
res.status(401).json({ message: 'Invalid or expired token' }); | ||
} | ||
}; | ||
|
||
// export const isAdmin = async ( | ||
// req: Request, | ||
// res: Response, | ||
// next: NextFunction, | ||
// ) => { | ||
// try { | ||
// const userId = req.params.userId; | ||
// const user = await UserModel.findById(userId); | ||
// if (user?.isAdmin) { | ||
// next(); | ||
// } else { | ||
// throw new ForbiddenError('Only Admins are allowed'); | ||
// } | ||
// } catch (err: any) { | ||
// console.error(err); | ||
// res.status(403).json({ message: err.message }); | ||
// } | ||
// }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { z } from 'zod'; | ||
|
||
export const productSchema = z.object({ | ||
name: z.string().min(1), | ||
stock: z.coerce.number().min(0), | ||
reorderLevel: z.coerce.number().min(0), | ||
salesPrice: z.coerce.number().min(0), | ||
costPrice: z.coerce.number().min(0), | ||
}); | ||
|
||
export type TProductSchema = z.infer<typeof productSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters