-
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
8 changed files
with
129 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* @swagger | ||
* tags: | ||
* name: Product | ||
* description: API for managing products | ||
*/ | ||
|
||
/** | ||
* @swagger | ||
* /product: | ||
* post: | ||
* summary: Add new product | ||
* description: Adds a new product with its details. | ||
* tags: [Product] | ||
* requestBody: | ||
* required: true | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* name: | ||
* type: string | ||
* example: "Product Name" # Example product name | ||
* stock: | ||
* type: number | ||
* example: 100 # Example stock value | ||
* reorderLevel: | ||
* type: number | ||
* example: 10 # Example reorder level | ||
* salesPrice: | ||
* type: number | ||
* example: 99.99 # Example sales price | ||
* costPrice: | ||
* type: number | ||
* example: 79.99 # Example cost price | ||
* required: | ||
* - name | ||
* - stock | ||
* - reorderLevel | ||
* - salesPrice | ||
* - costPrice | ||
* responses: | ||
* 201: | ||
* $ref: '#/components/responses/201' | ||
* 400: | ||
* $ref: '#/components/responses/400' | ||
* 500: | ||
* $ref: '#/components/responses/500' | ||
*/ |
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 |
---|---|---|
@@ -1,11 +1,28 @@ | ||
import ProductModel from './product.model'; | ||
import { productSchema, TProductSchema } from '../../../schema/product.schema'; | ||
import { BadRequestError } from '../../../utils/exceptions'; | ||
|
||
export class ProductService { | ||
async addNewProduct(payload: TProductSchema) { | ||
async addNewProduct(payload: TProductSchema, user: string) { | ||
const { data, success } = productSchema.safeParse(payload); | ||
if (!success) throw new BadRequestError('Invalid payload format'); | ||
const newProduct = new ProductModel(data); | ||
|
||
const batches = [ | ||
{ | ||
batchNo: 1, | ||
qty: data.stock, | ||
salesPrice: data.salesPrice, | ||
costPrice: data.costPrice, | ||
}, | ||
]; | ||
|
||
const newProduct = new ProductModel({ | ||
user, | ||
name: data.name, | ||
reorderLevel: data.reorderLevel, | ||
batches, | ||
}); | ||
|
||
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 |
---|---|---|
@@ -1,47 +1,27 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { Response, NextFunction } from 'express'; | ||
import jwt from 'jsonwebtoken'; | ||
import { ForbiddenError, UnauthorizedError } from './utils/exceptions'; | ||
import UserModel from './app/user/user.model'; | ||
import { UnauthorizedError } from './utils/exceptions'; | ||
import { IRequestWithUser } from './types/express'; | ||
|
||
export const isAuthencticated = ( | ||
req: Request, | ||
req: IRequestWithUser, | ||
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(); | ||
const decoded = jwt.verify(token, process.env.ACCESS_TOKEN_SECRET); | ||
req.userId = (decoded as any).id; | ||
return next(); | ||
} catch (err) { | ||
console.error(err); | ||
res.status(401).json({ message: 'Invalid or expired token' }); | ||
return res.status(401).json({ error: '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,5 @@ | ||
import { Request } from 'express'; | ||
|
||
export interface IRequestWithUser extends Request { | ||
userId?: string; | ||
} |
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 |
---|---|---|
@@ -1,11 +1,19 @@ | ||
import { NextFunction, Request, Response, RequestHandler } from 'express'; | ||
import { NextFunction, Response, RequestHandler } from 'express'; | ||
import { IRequestWithUser } from '../types/express'; | ||
|
||
export const asyncWrapper = (handler: RequestHandler) => { | ||
return async (req: Request, res: Response, next: NextFunction) => { | ||
export const asyncWrapper = ( | ||
handler: ( | ||
req: IRequestWithUser, | ||
res: Response, | ||
next: NextFunction, | ||
) => Promise<any>, | ||
): RequestHandler => { | ||
return async (req: IRequestWithUser, res: Response, next: NextFunction) => { | ||
try { | ||
await handler(req, res, next); | ||
} catch (error) { | ||
next(error); // Pass any caught errors to Express error handler | ||
console.error(error); | ||
next(error); | ||
} | ||
}; | ||
}; |