Skip to content

Commit 0cb309f

Browse files
committed
feature: 배치, soft delete 기능과 is active false 의 추가로 이에 모든 조건에 해당 조건 추가
1 parent 5f14e86 commit 0cb309f

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

src/repositories/__test__/post.repo.test.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ describe('PostRepository', () => {
5656
expect(result.posts).toEqual(mockPosts);
5757
expect(result.posts[0].id).toBeGreaterThan(result.posts[1].id);
5858
});
59+
60+
it('쿼리에 is_active = TRUE 조건이 포함되어야 한다', async () => {
61+
const mockPosts = [
62+
{ id: 1, post_released_at: '2025-03-01T00:00:00Z', daily_view_count: 10, daily_like_count: 5 },
63+
];
64+
65+
mockPool.query.mockResolvedValue({
66+
rows: mockPosts,
67+
rowCount: mockPosts.length,
68+
command: '',
69+
oid: 0,
70+
fields: [],
71+
} as QueryResult);
72+
73+
await repo.findPostsByUserId(1);
74+
75+
// 쿼리 호출 확인
76+
expect(mockPool.query).toHaveBeenCalledWith(
77+
expect.stringContaining("p.is_active = TRUE"),
78+
expect.anything()
79+
);
80+
});
5981
});
6082

6183
describe('findPostsByUserIdWithGrowthMetrics', () => {
@@ -157,6 +179,28 @@ describe('PostRepository', () => {
157179
mockPool.query.mockRejectedValue(new Error('DB connection failed'));
158180
await expect(repo.findPostsByUserIdWithGrowthMetrics(1)).rejects.toThrow(DBError);
159181
});
182+
183+
it('쿼리에 is_active = TRUE 조건이 포함되어야 한다', async () => {
184+
const mockPosts = [
185+
{ id: 1, view_growth: 20, like_growth: 5 },
186+
];
187+
188+
mockPool.query.mockResolvedValue({
189+
rows: mockPosts,
190+
rowCount: mockPosts.length,
191+
command: '',
192+
oid: 0,
193+
fields: [],
194+
} as QueryResult);
195+
196+
await repo.findPostsByUserIdWithGrowthMetrics(1);
197+
198+
// 쿼리 호출 확인
199+
expect(mockPool.query).toHaveBeenCalledWith(
200+
expect.stringContaining("p.is_active = TRUE"),
201+
expect.anything()
202+
);
203+
});
160204
});
161205

162206
describe('getTotalPostCounts', () => {
@@ -172,6 +216,24 @@ describe('PostRepository', () => {
172216
const count = await repo.getTotalPostCounts(1);
173217
expect(count).toBe(10);
174218
});
219+
220+
it('is_active = TRUE인 게시물만 카운트해야 한다', async () => {
221+
mockPool.query.mockResolvedValue({
222+
rows: [{ count: '5' }],
223+
rowCount: 1,
224+
command: '',
225+
oid: 0,
226+
fields: [],
227+
} as QueryResult);
228+
229+
await repo.getTotalPostCounts(1);
230+
231+
// 쿼리 호출 확인
232+
expect(mockPool.query).toHaveBeenCalledWith(
233+
expect.stringContaining("is_active = TRUE"),
234+
expect.anything()
235+
);
236+
});
175237
});
176238

177239
describe('에러 발생 시 처리', () => {
@@ -204,6 +266,24 @@ describe('PostRepository', () => {
204266
const result = await repo.getYesterdayAndTodayViewLikeStats(1);
205267
expect(result).toEqual(mockStats);
206268
});
269+
270+
it('is_active = TRUE인 게시물의 통계만 반환해야 한다', async () => {
271+
mockPool.query.mockResolvedValue({
272+
rows: [{ daily_view_count: 20, daily_like_count: 15 }],
273+
rowCount: 1,
274+
command: '',
275+
oid: 0,
276+
fields: [],
277+
} as QueryResult);
278+
279+
await repo.getYesterdayAndTodayViewLikeStats(1);
280+
281+
// 쿼리 호출 확인
282+
expect(mockPool.query).toHaveBeenCalledWith(
283+
expect.stringContaining("p.is_active = TRUE"),
284+
expect.anything()
285+
);
286+
});
207287
});
208288

209289
describe('findPostByPostId', () => {

src/repositories/post.repository.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export class PostRepository {
7878
WHERE (date AT TIME ZONE 'Asia/Seoul' AT TIME ZONE 'UTC')::date = (NOW() AT TIME ZONE 'UTC' - INTERVAL '1 day')::date
7979
) yesterday_stats ON p.id = yesterday_stats.post_id
8080
WHERE p.user_id = $1
81+
AND p.is_active = TRUE
8182
AND (pds.post_id IS NOT NULL OR yesterday_stats.post_id IS NOT NULL)
8283
${cursorCondition}
8384
ORDER BY ${orderBy}
@@ -182,6 +183,7 @@ export class PostRepository {
182183
WHERE (date AT TIME ZONE 'Asia/Seoul' AT TIME ZONE 'UTC')::date = (NOW() AT TIME ZONE 'UTC' - INTERVAL '1 day')::date
183184
) yesterday_stats ON p.id = yesterday_stats.post_id
184185
WHERE p.user_id = $1
186+
AND p.is_active = TRUE
185187
AND (pds.post_id IS NOT NULL OR yesterday_stats.post_id IS NOT NULL)
186188
${cursorCondition}
187189
ORDER BY ${orderByExpression}
@@ -213,7 +215,7 @@ export class PostRepository {
213215

214216
async getTotalPostCounts(id: number) {
215217
try {
216-
const query = 'SELECT COUNT(*) FROM "posts_post" WHERE user_id = $1';
218+
const query = 'SELECT COUNT(*) FROM "posts_post" WHERE user_id = $1 AND is_active = TRUE';
217219
const result = await this.pool.query(query, [id]);
218220
return parseInt(result.rows[0].count, 10);
219221
} catch (error) {
@@ -244,6 +246,7 @@ export class PostRepository {
244246
WHERE (date AT TIME ZONE 'Asia/Seoul' AT TIME ZONE 'UTC')::date = (NOW() AT TIME ZONE 'UTC' - INTERVAL '1 day')::date
245247
) yesterday_stats ON p.id = yesterday_stats.post_id
246248
WHERE p.user_id = $1
249+
AND p.is_active = TRUE
247250
`;
248251
const values = [userId];
249252

@@ -304,6 +307,7 @@ export class PostRepository {
304307
FROM posts_post p
305308
JOIN posts_postdailystatistics pds ON p.id = pds.post_id
306309
WHERE p.post_uuid = $1
310+
AND p.is_active = TRUE
307311
AND (pds.date AT TIME ZONE 'Asia/Seoul' AT TIME ZONE 'UTC')::date >= ($2 AT TIME ZONE 'Asia/Seoul' AT TIME ZONE 'UTC')::date
308312
AND (pds.date AT TIME ZONE 'Asia/Seoul' AT TIME ZONE 'UTC')::date <= ($3 AT TIME ZONE 'Asia/Seoul' AT TIME ZONE 'UTC')::date
309313
ORDER BY pds.date ASC

0 commit comments

Comments
 (0)