Skip to content

Commit

Permalink
Merge pull request #92 from atlp-rwanda/187906547-bug-fix-stock-updat…
Browse files Browse the repository at this point in the history
…e-password-reset-link

finishes [#187906547] stock update and password reset link
  • Loading branch information
niyontwali authored Jul 8, 2024
2 parents e0e53ee + ec7ccad commit 97ba7e6
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ gender=""
FRONT_END_BASEURL="";

STRIPE_SECRET_KEY=""
STRIPE_WEBHOOK_SECRET=""
STRIPE_WEBHOOK_SECRET=""
CLIENT_URL
2 changes: 1 addition & 1 deletion src/controllers/authController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export const forgotPassword = async (req: Request, res: Response) => {
const token = await userToken(user.id, user.email);

// Send email with token
const link = `${process.env.URL_HOST}/api/auth/reset-password/${token}`;
const link = `${process.env.CLIENT_URL}/reset-password/${token}`;

await sendEmail('reset_password', {
name: `${user.firstName} ${user.lastName}`,
Expand Down
21 changes: 4 additions & 17 deletions src/controllers/cartController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { Size } from '../database/models/Size';
import CartsProducts from '../database/models/cartsProducts';
import { validateFields } from '../validations';
import { Transaction } from 'sequelize';
import { checkStockSize, updateStock } from '../helpers/stockSizeManagers';

const addCartItem = async (req: Request, res: Response): Promise<void> => {
const { productId, sizeId, quantity } = req.body;
const { id: userId } = req.user as User;
Expand All @@ -36,18 +34,7 @@ const addCartItem = async (req: Request, res: Response): Promise<void> => {
res.status(404).json({ ok: false, message: 'Size not found for this product' });
return;
}
const stock = await checkStockSize(sizeId, productId, quantity);
if (stock <= 0) {
const product = await Product.findByPk(productId, {
include: [{ model: Size, as: 'sizes', where: { id: sizeId }, attributes: ['quantity'] }],
});

res
.status(404)
.json({ ok: false, message: `Our stock has ${product?.sizes[0].quantity} product(s) of this size only!` });
return;
}
await updateStock(sizeId, productId, stock);
// Find the cart for the current user
const cart = await Cart.findOne({ where: { userId }, transaction });
if (cart) {
Expand Down Expand Up @@ -102,12 +89,12 @@ const updateCartItem = async (req: Request, res: Response): Promise<void> => {

const transaction = await sequelize.transaction();
try {
const cartItem: any = await Cart.findOne({
const cartItem: Cart | null = await Cart.findOne({
where: { userId },
transaction,
});

if (!cartItem) {
if (cartItem === null) {
res.status(404).json({ ok: false, message: 'Cart not found' });
return;
}
Expand Down Expand Up @@ -139,7 +126,7 @@ const getCartItems = async (req: Request, res: Response): Promise<void> => {

const transaction = await sequelize.transaction();
try {
const cart: any = await Cart.findOne({
const cart: Cart | null = await Cart.findOne({
where: { userId },
include: [
{
Expand All @@ -157,7 +144,7 @@ const getCartItems = async (req: Request, res: Response): Promise<void> => {
],
transaction,
});
if (!cart) {
if (cart === null) {
res.status(404).json({ ok: false, message: 'Cart not found' });
return;
}
Expand Down
7 changes: 2 additions & 5 deletions src/controllers/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,14 @@ export const editUserRole = async (req: Request, res: Response) => {
export const editUser = async (req: Request, res: Response) => {
try {
const { firstName, lastName, gender, phoneNumber } = req.body;

const user = await User.findOne({ where: { id: req.params.id } });

// eslint-disable-next-line @typescript-eslint/no-explicit-any
let uploadedImage: any;
if (!req.file) {
res.status(400).json({ ok: false, error: 'Profile Image required.' });
return;
}
if (req.file) {
uploadedImage = await uploadImage(req.file.buffer);
}
const uploadedImage = await uploadImage(req.file.buffer);
const updatedFields = {
firstName: firstName || user?.firstName,
lastName: lastName || user?.lastName,
Expand Down
6 changes: 3 additions & 3 deletions src/database/models/Size.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Model, Optional, DataTypes, UUIDV4 } from 'sequelize';
import { Model, DataTypes, UUIDV4 } from 'sequelize';
import sequelize from './index';
export interface SizeAttributes {
id?: number;
id?: string;
size?: string;
price: number;
quantity?: number;
Expand All @@ -14,7 +14,7 @@ export interface SizeAttributes {
}

export class Size extends Model<SizeAttributes> implements SizeAttributes {
public id!: number;
public id!: string;
public size!: string;
public price!: number;
public quantity!: number;
Expand Down

0 comments on commit 97ba7e6

Please sign in to comment.