Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "refactor: UsersService.setOverDueDay" #567

Merged
merged 1 commit into from
Jul 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions backend/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,24 @@ export default class UsersService {
this.usersRepository = new UsersRepository();
}

/**
* 기존 사용자 배열에 대출과 연체 정보를 추가하여 반환합니다.
*
* @returns 사용자의 대출 정보를 포함한 사용자 정보 배열
*/
async withLendingInfo(users: models.User[]): Promise<models.User[]> {
const usersIdList = users.map((user) => ({ userId: user.id }));
async setOverDueDay(items: models.User[]): Promise<models.User[]> {
const usersIdList = items.map((user: models.User) => ({ userId: user.id }));
const lending = await this.usersRepository
.getLending(usersIdList) as unknown as models.Lending[];

return users.map((user) => {
const lendings = lending.filter((lend) => lend.userId === user.id);
const overDueDay = lendings.reduce((acc, cur) => acc + cur.overDueDay, 0);
return { ...user, lendings, overDueDay };
});
if (items) {
return items.map((item: models.User) => {
const rtnObj: models.User = Object.assign(item);
rtnObj.lendings = lending.filter((lend) => lend.userId === item.id);
rtnObj.overDueDay = 0;
if (rtnObj.lendings.length) {
rtnObj.lendings.forEach((lend: models.Lending) => {
rtnObj.overDueDay += (+lend.overDueDay);
});
}
return rtnObj;
});
}
return items;
}

async userLendings(userId: number) {
Expand All @@ -43,7 +46,7 @@ export default class UsersService {
{ nickname: Like(`%${nicknameOrEmail}%`) },
{ email: Like(`%${nicknameOrEmail}`) },
], limit, page);
const setItems = await this.withLendingInfo(items);
const setItems = await this.setOverDueDay(items);
const meta: types.Meta = {
totalItems: count,
itemCount: setItems.length,
Expand All @@ -56,7 +59,7 @@ export default class UsersService {

async searchUserById(id: number) {
let items = (await this.usersRepository.searchUserBy({ id }, 0, 0))[0];
items = await this.withLendingInfo(items);
items = await this.setOverDueDay(items);
return { items };
}

Expand All @@ -77,7 +80,7 @@ export default class UsersService {

async searchAllUsers(limit: number, page: number) {
const [items, count] = await this.usersRepository.searchUserBy(1, limit, page);
const setItems = await this.withLendingInfo(items);
const setItems = await this.setOverDueDay(items);
const meta: types.Meta = {
totalItems: count,
itemCount: setItems.length,
Expand Down