Skip to content

Commit 22c2513

Browse files
authored
Merge pull request #163 from innovationacademy-kr/feat-#154-2
테스트케이스 추가 및 중복제거 로직 추가
2 parents 3ab759b + 2286ffb commit 22c2513

File tree

2 files changed

+122
-4
lines changed

2 files changed

+122
-4
lines changed

src/tag-log-v2/tag-log-v2.service.spec.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,38 @@ describe('Tag Log (v2) unit 테스트', () => {
8181
expect(resultPairs).toHaveProperty('[0].durationSecond', 50 * 60); // 50 min
8282
});
8383

84+
test('특정 일수 내 입-퇴실 짝이 맞긴 하지만, 중복으로 입실이 찍힌 케이스', async () => {
85+
// given
86+
const taglogs: TagLogDto[] = [
87+
{
88+
idx: 1,
89+
tag_at: new Date('2023-03-19T05:10Z'), // 오후 2:10:00
90+
card_id: '',
91+
device_id: 1,
92+
},
93+
{
94+
idx: 2,
95+
tag_at: new Date('2023-03-19T05:20Z'), // 오후 2:20:00
96+
card_id: '',
97+
device_id: 1,
98+
},
99+
{
100+
idx: 3,
101+
tag_at: new Date('2023-03-19T06:00Z'), // 오후 3:00:00
102+
card_id: '',
103+
device_id: 2,
104+
},
105+
];
106+
107+
// when
108+
const resultPairs = tagLogService.getAllPairsByTagLogs(taglogs, pairs);
109+
110+
//then
111+
expect(resultPairs).toHaveLength(2);
112+
expect(resultPairs).toHaveProperty('[0].durationSecond', 40 * 60); // 40 min
113+
expect(resultPairs).toHaveProperty('[1].outTimeStamp', null); // 짝이 맞지 않음.
114+
});
115+
84116
test('입-퇴실 짝이 월 경계 자정을 넘어가는 케이스', async () => {
85117
// given
86118
const taglogs: TagLogDto[] = [
@@ -158,4 +190,64 @@ describe('Tag Log (v2) unit 테스트', () => {
158190
expect(resultPairs).toHaveProperty('[0].durationSecond', 1); // 1 sec
159191
});
160192
});
193+
describe('removeDuplicates', () => {
194+
test('중복되지 않은 태그 로그', async () => {
195+
// given
196+
const taglogs: TagLogDto[] = [
197+
{
198+
idx: 1,
199+
tag_at: new Date('2023-03-19T05:10Z'), // 오후 2:10:00
200+
card_id: '',
201+
device_id: 1,
202+
},
203+
{
204+
idx: 2,
205+
tag_at: new Date('2023-03-19T06:00Z'), // 오후 3:00:00
206+
card_id: '',
207+
device_id: 2,
208+
},
209+
];
210+
211+
// when
212+
const resultPairs = tagLogService.removeDuplicates(taglogs);
213+
214+
//then
215+
expect(resultPairs).toHaveLength(2);
216+
});
217+
test('중복되는 태그 로그', async () => {
218+
// given
219+
const taglogs: TagLogDto[] = [
220+
{
221+
idx: 1,
222+
tag_at: new Date('2023-03-19T05:10Z'), // 오후 2:10:00
223+
card_id: '',
224+
device_id: 1,
225+
},
226+
{
227+
idx: 2,
228+
tag_at: new Date('2023-03-19T06:00Z'), // 오후 3:00:00
229+
card_id: '',
230+
device_id: 2,
231+
},
232+
{
233+
idx: 3,
234+
tag_at: new Date('2023-03-19T06:00Z'), // 오후 3:00:00
235+
card_id: '',
236+
device_id: 2,
237+
},
238+
{
239+
idx: 4,
240+
tag_at: new Date('2023-03-19T06:00Z'), // 오후 3:00:00
241+
card_id: '',
242+
device_id: 2,
243+
},
244+
];
245+
246+
// when
247+
const resultPairs = tagLogService.removeDuplicates(taglogs);
248+
249+
//then
250+
expect(resultPairs).toHaveLength(2);
251+
});
252+
});
161253
});

src/tag-log-v2/tag-log-v2.service.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ export class TagLogService {
2222
private dateCalculator: DateCalculator,
2323
) {}
2424

25+
/**
26+
* DB에서 중복된 태그 기록이 올 경우, 중복된 태그 기록을 필터링합니다.
27+
* 중복 기준은 태그 시간, 태그 ID, 태그 기기입니다.
28+
*
29+
* @param taglogs TagLogDto[]
30+
* @return TagLogDto[]
31+
*/
32+
removeDuplicates(taglogs: TagLogDto[]): TagLogDto[] {
33+
const comp = (a: TagLogDto, b: TagLogDto) =>
34+
a.tag_at.getTime() === b.tag_at.getTime() &&
35+
a.card_id === b.card_id &&
36+
a.device_id === b.device_id;
37+
return taglogs.reduce(
38+
(acc, tag) => (acc.find((val) => comp(val, tag)) ? acc : [...acc, tag]),
39+
[],
40+
);
41+
}
42+
2543
/**
2644
* 카드 태그 로그에 대해 로그 맨 앞, 맨 뒤 원소가 잘려 있다면 앞, 뒤에 가상의 입출입 로그를 삽입합니다.
2745
* 삽입하는 로그는 짝 여부에 관계없이 전후 원소를 삽입합니다.
@@ -318,8 +336,10 @@ export class TagLogService {
318336

319337
const taglogs = await this.getAllTagLogsByPeriod(userId, tagStart, tagEnd);
320338

339+
const newtaglogs = this.removeDuplicates(taglogs);
340+
321341
//짝이 안맞는 로그도 null과 pair를 만들어 반환한다.
322-
const resultPairs = this.getAllPairsByTagLogs(taglogs, pairs);
342+
const resultPairs = this.getAllPairsByTagLogs(newtaglogs, pairs);
323343

324344
return resultPairs;
325345
}
@@ -351,7 +371,9 @@ export class TagLogService {
351371
today,
352372
);
353373

354-
const resultPairs = this.getAllPairsByTagLogs(taglogs, pairs);
374+
const newtaglogs = this.removeDuplicates(taglogs);
375+
376+
const resultPairs = this.getAllPairsByTagLogs(newtaglogs, pairs);
355377

356378
const ret: number[] = [];
357379

@@ -395,7 +417,9 @@ export class TagLogService {
395417
// this.logger.debug(`taglogs: ${element.device_id}, ${element.tag_at}`);
396418
//});
397419

398-
const resultPairs = this.getAllPairsByTagLogs(taglogs, pairs);
420+
const newtaglogs = this.removeDuplicates(taglogs);
421+
422+
const resultPairs = this.getAllPairsByTagLogs(newtaglogs, pairs);
399423

400424
//resultPairs.forEach((element) => {
401425
// this.logger.debug(
@@ -429,7 +453,9 @@ export class TagLogService {
429453
today,
430454
);
431455

432-
const resultPairs = this.getAllPairsByTagLogs(taglogs, pairs);
456+
const newtaglogs = this.removeDuplicates(taglogs);
457+
458+
const resultPairs = this.getAllPairsByTagLogs(newtaglogs, pairs);
433459

434460
const ret: number[] = [];
435461

0 commit comments

Comments
 (0)