Skip to content

Commit

Permalink
Merge branch 'fix-google-auth' of https://github.com/atlp-rwanda/knig…
Browse files Browse the repository at this point in the history
…hts-ecomm-be into fix-google-auth
  • Loading branch information
maxCastro1 committed Jun 25, 2024
2 parents 3291418 + 3555b41 commit cba9ca4
Show file tree
Hide file tree
Showing 13 changed files with 4,979 additions and 3,692 deletions.
8,571 changes: 4,902 additions & 3,669 deletions model.nlp

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions src/__test__/categories.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import request from 'supertest';
import { app, server } from '../index';
import { createConnection, getRepository} from 'typeorm';
import { cleanDatabase } from './test-assets/DatabaseCleanup';
import { Category } from '../entities/Category';

beforeAll(async () => {
await createConnection();
});

jest.setTimeout(20000);

afterAll(async () => {
await cleanDatabase();
server.close();
});


describe('GET /categories', () => {
it('should return all categories', async () => {
const categoryRepository = getRepository(Category);
await categoryRepository.save([
{ name: 'Category 1' },
{ name: 'Category 2' },
]);

const response = await request(app).get('/product/categories');
console.log(response.error)
expect(response.status).toBe(200);
expect(response.body.status).toBe('success');
expect(response.body.categories).toHaveLength(2);
expect(response.body.categories[0].name).toBe('Category 1');
expect(response.body.categories[1].name).toBe('Category 2');
});

it('should handle errors gracefully', async () => {
const response = await request(app).get('/product/categories');

expect(response.status).toBe(200);
expect(response.body.status).toBe('success');
expect(response.body.categories).toHaveLength(2);
});
});
6 changes: 3 additions & 3 deletions src/__test__/searchProduct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ describe('Get single product', () => {
.get(`/product/${expiredProductId}`)
.set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`);

expect(response.status).toBe(400);
expect(response.body.status).toBe('error');
expect(response.body.message).toBe('Product expired');
expect(response.status).toBe(200);
expect(response.body.status).toBe('success');
expect(response.body.product).toBeDefined();
});

it('should return 400 for invalid product id', async () => {
Expand Down
4 changes: 4 additions & 0 deletions src/controllers/productController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
searchProductService,
listAllProductsService,
confirmPayment,
getAllCategories
} from '../services';

export const readProduct = async (req: Request, res: Response) => {
Expand Down Expand Up @@ -57,4 +58,7 @@ export const searchProduct = async (req: Request, res: Response) => {
};
export const Payment = async (req: Request, res: Response) => {
await confirmPayment(req, res);
};
export const getAllCategory = async (req: Request, res: Response) => {
await getAllCategories(req, res);
};
4 changes: 4 additions & 0 deletions src/routes/ProductRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
getBuyerVendorOrders,
getSingleBuyerVendorOrder,
updateBuyerVendorOrder,
getAllCategory
} from '../controllers';
const router = Router();

Expand All @@ -33,6 +34,7 @@ router.get('/all', listAllProducts);
router.get('/recommended', authMiddleware as RequestHandler, hasRole('BUYER'), getRecommendedProducts);
router.get('/collection', authMiddleware as RequestHandler, hasRole('VENDOR'), readProducts);
router.get('/', authMiddleware as RequestHandler, hasRole('BUYER'), readProducts);
router.get('/categories', getAllCategory);
router.get('/:id', singleProduct);
router.get('/collection/:id', authMiddleware as RequestHandler, hasRole('VENDOR'), readProduct);
router.post('/', authMiddleware as RequestHandler, hasRole('VENDOR'), upload.array('images', 10), createProduct);
Expand All @@ -59,4 +61,6 @@ router.put('/admin/orders/:id', authMiddleware as RequestHandler, hasRole('ADMIN
router.post('/payment/:id', authMiddleware as RequestHandler, hasRole('BUYER'), Payment);




export default router;
3 changes: 2 additions & 1 deletion src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export * from './productServices/productStatus';
export * from './productServices/viewSingleProduct';
export * from './productServices/searchProduct';
export * from './productServices/payment';
export * from './productServices/getCategories';

// Buyer wishlist services
export * from './wishListServices/addProduct';
Expand Down Expand Up @@ -49,4 +50,4 @@ export * from './notificationServices/deleteNotification';
export * from './notificationServices/updateNotification';

// chatbot
export * from './chatbotServices/chatBot';
export * from './chatbotServices/chatBot';
15 changes: 15 additions & 0 deletions src/services/productServices/getCategories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Request, Response } from 'express';
import { getRepository } from 'typeorm';
import { Category } from '../../entities/Category';

export const getAllCategories = async (req: Request, res: Response) => {
try {
const categoryRepository = getRepository(Category);
const categories = await categoryRepository.find();

res.status(200).json({ status: 'success', categories });
} catch (error) {
console.error('Error fetching categories:', error);
res.status(500).json({ status: 'error', message: 'Error fetching categories' });
}
};
15 changes: 5 additions & 10 deletions src/services/productServices/listAllProductsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Request, Response } from 'express';
import { Product } from '../../entities/Product';
import { getRepository } from 'typeorm';
import { responseError, responseSuccess } from '../../utils/response.utils';
import { validate } from 'uuid';

export const listAllProductsService = async (req: Request, res: Response) => {
try {
Expand All @@ -13,13 +12,12 @@ export const listAllProductsService = async (req: Request, res: Response) => {

const productRepository = getRepository(Product);
const products = await productRepository.find({
where: {
categories: {
name: category as string,
},
},
where: category ? { categories: { name: category as string } } : {},
skip,
take: limit,
order: {
createdAt: 'DESC',
},
relations: ['categories', 'vendor', 'feedbacks'],
select: {
vendor: {
Expand All @@ -33,9 +31,6 @@ export const listAllProductsService = async (req: Request, res: Response) => {
},
});

if (products.length < 1) {
return responseSuccess(res, 200, 'No products found');
}
if (products.length < 1) {
return responseSuccess(res, 200, 'No products found');
}
Expand All @@ -44,4 +39,4 @@ export const listAllProductsService = async (req: Request, res: Response) => {
} catch (error) {
responseError(res, 400, (error as Error).message);
}
};
};
1 change: 0 additions & 1 deletion src/services/productServices/productStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export const productStatusServices = async (req: Request, res: Response) => {
const { id } = req.params;

if (isAvailable === undefined) {
console.log('Error: Please fill all the required fields');
return responseError(res, 400, 'Please fill all t he required fields');
}

Expand Down
1 change: 0 additions & 1 deletion src/services/productServices/searchProduct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export const searchProductService = async (req: Request, res: Response) => {
const { name, sortBy, sortOrder, page = 1, limit = 10 }: SearchProductParams = req.query as any;
try {
if (!name) {
console.log("no name");
return res.status(400).json({ status: 'error', error: 'Please provide a search term' });
}

Expand Down
4 changes: 0 additions & 4 deletions src/services/productServices/viewSingleProduct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ export const viewSingleProduct = async (req: Request, res: Response) => {
if (!product) {
return res.status(404).send({ status: 'error', message: 'Product not found' });
}

if (product.expirationDate && new Date(product.expirationDate) < new Date()) {
return res.status(400).json({ status: 'error', message: 'Product expired' });
}
res.status(200).json({ status: 'success', product: product });
}
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/services/userServices/sendResetPasswordLinkService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const sendPasswordResetLinkService = async (req: Request, res: Response)
password has been generated for you. To reset your password, click the
following link and follow the instructions.
</p>
<a href="${process.env.FRONTEND_URL}/${process.env.PASSWORD_ROUTE}?userid=${existingUser.id}&email=${existingUser.email}" target="_blank"
<a href="${process.env.CLIENT_URL}/reset-password?userid=${existingUser.id}&email=${existingUser.email}" target="_blank"
style="background:#20e277;text-decoration:none !important; font-weight:500; margin-top:35px; color:#fff;text-transform:uppercase; font-size:14px;padding:10px 24px;display:inline-block;border-radius:50px;">Reset
Password</a>
</td>
Expand Down
2 changes: 0 additions & 2 deletions src/services/userServices/userResendOTP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ export const userResendOtpService = async (req: Request, res: Response) => {
const { email } = req.body;

if (!email) {
console.log('No email address provided');
return res.status(400).json({ status: 'error', message: 'Please provide an email' });
}

const userRepository = getRepository(User);
const user = await userRepository.findOneBy({ email });

if (!user) {
console.log('User not found');
return res.status(404).json({ status: 'error', message: 'Incorrect email' });
}

Expand Down

0 comments on commit cba9ca4

Please sign in to comment.