Skip to content

Commit

Permalink
feat: add buy & update comic api
Browse files Browse the repository at this point in the history
  • Loading branch information
conganhhcmus committed Mar 16, 2024
1 parent 5d276d3 commit 0198759
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/constants/paging.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const DEFAULT_PAGE = 1;

export const DEFAULT_PAGE_SIZE = 20;
export const DEFAULT_PAGE_SIZE = 5;

export const DEFAULT_COMIC_PAGE_SIZE = 20;
27 changes: 25 additions & 2 deletions src/controllers/payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,39 @@ export const depositAccount = async (req: Request, res: Response) => {
return res.status(200).json(result);
};

export const buyChapter = async (req: Request, res: Response) => {
const amount = parseInt(req.body.amount, 10) || 0;
const chapterId = (req.body.chapterId as string) || '';
const payload = req['identity'] as UserJwtPayload;

if (amount === 0 || chapterId === '') {
throw new BadRequestError(INVALID_PARAMETERS);
}

const result = await paymentService.buyChapter(amount, payload._id, chapterId);

return res.status(200).json(result);
};

export const getAllTransactionByUserId = async (req: Request, res: Response) => {
const { id } = req.params;
const { id: userId } = req.params;
const page = parseInt(req.query.page as string, 10) || 1;
const option = parseInt(req.query.option as string, 10) || 0;
const status = (req.query.status as string[]).map((x) => parseInt(x, 10) || 0);

const result = await paymentService.getAllTransactionByUserId(id, option, status, page);
const result = await paymentService.getAllTransactionByUserId(userId, option, status, page);

return res.status(200).json(result);
};

export const getAllBuyTransactionByUserId = async (req: Request, res: Response) => {
const { id: userId } = req.params;

const result = await paymentService.getAllBuyTransactionByUserId(userId);

return res.status(200).json(result);
};

export const getAllTransaction = async (req: Request, res: Response) => {
const page = parseInt(req.query.page as string, 10) || 1;
const option = parseInt(req.query.option as string, 10) || 0;
Expand Down
12 changes: 6 additions & 6 deletions src/repositories/comics.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { DEFAULT_PAGE_SIZE } from '@/constants/paging';
import { DEFAULT_COMIC_PAGE_SIZE, DEFAULT_PAGE_SIZE } from '@/constants/paging';
import ComicsModel from '@/models/comics';
import { Types } from 'mongoose';
import { Comic } from '@/types/comics';

const getComicByPageAndQuery = async (page: number, query: {}) => {
const getComicByPageAndQuery = async (page: number, query: {}, pageSize: number = DEFAULT_COMIC_PAGE_SIZE) => {
const total = await ComicsModel.countDocuments().exec();
const comics = await ComicsModel.aggregate([
{
Expand All @@ -29,17 +29,17 @@ const getComicByPageAndQuery = async (page: number, query: {}) => {
as: 'genres',
},
},
{ $skip: DEFAULT_PAGE_SIZE * page - DEFAULT_PAGE_SIZE },
{ $limit: DEFAULT_PAGE_SIZE },
{ $skip: pageSize * page - pageSize },
{ $limit: pageSize },
{ $match: query },
]);

return { data: comics, totalPage: Math.ceil(total / DEFAULT_PAGE_SIZE), currentPage: page };
return { data: comics, totalPage: Math.ceil(total / pageSize), currentPage: page };
};

export const getComics = async (page: number, q: string) => {
const query = !!q ? { name: { $regex: '.*' + q + '.*', $options: 'i' } } : {};
return getComicByPageAndQuery(page, query);
return getComicByPageAndQuery(page, query, DEFAULT_PAGE_SIZE);
};

export const getRecommendComics = async (page: number) => {
Expand Down
13 changes: 13 additions & 0 deletions src/repositories/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DEFAULT_PAGE_SIZE } from '@/constants/paging';
import { TransactionStatus, TransactionType } from '@/constants/payment';
import TransactionModel from '@/models/transaction';
import { TransactionResponse } from '@/types/transaction';
import moment from 'moment';
Expand Down Expand Up @@ -42,5 +43,17 @@ export const getAllTransactionByUserId = async (userId: string, option: number,
return { data: transaction, totalPage: Math.ceil(total / DEFAULT_PAGE_SIZE), currentPage: page };
};

export const getAllBuyTransactionByUserId = async (userId: string) => {
const query = {
type: TransactionType.buy,
status: { $in: TransactionStatus.success },
targetId: new Types.ObjectId(userId),
};
const total = await TransactionModel.countDocuments().exec();
const transaction = await TransactionModel.find(query);

return transaction;
};

export const updateTransactionById = (id: string, status: number) =>
TransactionModel.findOneAndUpdate({ _id: new Types.ObjectId(id) }, { $set: { status: status } }, { new: true });
13 changes: 12 additions & 1 deletion src/routers/payment.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import { depositAccount, getAllTransaction, getAllTransactionByUserId, updateTransactionById } from '@/controllers/payment';
import {
buyChapter,
depositAccount,
getAllBuyTransactionByUserId,
getAllTransaction,
getAllTransactionByUserId,
updateTransactionById,
} from '@/controllers/payment';
import { verifyAccessToken } from '@/middlewares/authToken';
import { isAdmin } from '@/middlewares/usersValidation';
import { Router } from 'express';

export default (router: Router) => {
router.post('/payment/deposit', verifyAccessToken, depositAccount);

router.post('/payment/buy', verifyAccessToken, buyChapter);

router.get('/transaction/:id', verifyAccessToken, getAllTransactionByUserId);

router.get('/transaction/buy/:id', verifyAccessToken, getAllBuyTransactionByUserId);

router.get('/transaction', verifyAccessToken, isAdmin, getAllTransaction);

router.post('/transaction/:id', verifyAccessToken, isAdmin, updateTransactionById);
Expand Down
23 changes: 23 additions & 0 deletions src/services/payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,35 @@ export const depositAccount = async (amount: number, userId: string) => {
return result;
};

export const buyChapter = async (amount: number, userId: string, chapterId: string) => {
const data = {
amount: -amount,
sourceId: chapterId,
targetId: userId,
type: TransactionType.buy,
status: TransactionStatus.success,
createTime: moment().utc().toDate(),
} as Transaction;

const result = await transactionRepository.createTransaction(data);

await usersRepository.updateWalletById(result.targetId as string, result.amount);

return result;
};

export const getAllTransactionByUserId = async (userId: string, option: number, status: number[], page: number) => {
const result = await transactionRepository.getAllTransactionByUserId(userId, option, status, page);

return result;
};

export const getAllBuyTransactionByUserId = async (userId: string) => {
const result = await transactionRepository.getAllBuyTransactionByUserId(userId);

return result;
};

export const getAllTransaction = async (option: number, status: number[], page: number) => {
const result = await transactionRepository.getAllTransaction(option, status, page);

Expand Down

0 comments on commit 0198759

Please sign in to comment.