Skip to content

Commit

Permalink
refactor: 비동기로 데이터 삽입하도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
niamu01 committed Jul 3, 2024
1 parent 44bc813 commit e1cf923
Showing 1 changed file with 44 additions and 43 deletions.
87 changes: 44 additions & 43 deletions src/external/where42/where42.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,62 +76,63 @@ export class Where42Service {

@Post('where42All2')
async where42All2(@Body() logins: string[]): Promise<Where42ResponseDto[]> {
const res: Where42ResponseDto[] = [];

const users = await this.userService.findUsersByLogins(logins);
const userMap = new Map<string, IdLoginDto>(
users.map((user) => [user.login, user]),
);

const res = [];
await Promise.all(
logins.map(async (login) => {
try {
const user = userMap.get(login);
if (!user) {
throw new BadRequestException('존재하지 않는 유저 ID입니다.');
}

const isAdmin = user.is_admin;
if (isAdmin) {
res.push({
login,
inoutState: null,
});
return;
}

const cards = await this.userService.findCardsByUserId(
user.user_id,
new Date('2019-01-01 00:00:00'),
new Date(), // NOTE: 대략 42 클러스터 오픈일부터 지금까지 조회
);

for (const login of logins) {
try {
const user = userMap.get(login);
if (!user) {
throw new BadRequestException('존재하지 않는 유저 ID입니다.');
}
const last = await this.tagLogRepository.findLatestTagLog(cards);
if (last === null) {
throw new ForbiddenException('태그 기록이 존재하지 않습니다.');
}

const isAdmin = user.is_admin;
const device = await this.deviceInfoRepository.getDeviceInfo(
last.device_id,
);
if (device === null) {
throw new ForbiddenException(
'등록되지 않은 기기에 태그하였습니다. 관리자에게 문의하세요.',
);
}

if (isAdmin) {
res.push({
login,
inoutState: device.inoutState,
});
} catch (e) {
this.logger.error(`정상적인 조회가 아님: ${login}`);
res.push({
login,
inoutState: null,
});
continue;
}

const cards = await this.userService.findCardsByUserId(
user.user_id,
new Date('2019-01-01 00:00:00'),
new Date(), // NOTE: 대략 42 클러스터 오픈일부터 지금까지 조회
);

const last = await this.tagLogRepository.findLatestTagLog(cards);
if (last === null) {
throw new ForbiddenException('태그 기록이 존재하지 않습니다.');
}

const device = await this.deviceInfoRepository.getDeviceInfo(
last.device_id,
);
if (device === null) {
throw new ForbiddenException(
'등록되지 않은 기기에 태그하였습니다. 관리자에게 문의하세요.',
);
}

res.push({
login,
inoutState: device.inoutState,
});
} catch (e) {
this.logger.error(`정상적인 조회가 아님: ${login}`);
res.push({
login,
inoutState: null,
});
}
}
}),
);

return res;
}
Expand Down

0 comments on commit e1cf923

Please sign in to comment.