Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion apps/identity/test/integration/directory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe('Identity company directory (Postgres จริง)', function () {
});

it('applies absolute review statistics only when entity_version increases', async () => {
const base: ReviewPublishedPayload = { review_id: randomUUID(), company_id: alphaId, rating: 5,
const base: ReviewPublishedPayload = { review_id: randomUUID(), company_id: alphaId, change: 'PUBLISHED', rating: 5,
company_review_count: 2, company_rating_sum: 9, entity_version: 2, published_at: new Date().toISOString() };
await db.$transaction((tx) => ratings.apply(base, tx));
await db.$transaction((tx) => ratings.apply({ ...base, company_review_count: 1, company_rating_sum: 1,
Expand Down
2 changes: 2 additions & 0 deletions apps/lifecycle/src/events/producers/review-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ export function buildReviewSubmitted(review: ReviewEventSource): ReviewSubmitted
export function buildReviewPublished(
review: Pick<ReviewEventSource, 'id' | 'companyId' | 'rating'>,
stats: { reviewCount: number; ratingSum: number; entityVersion: number },
change: ReviewPublishedPayload['change'],
at: Date,
): ReviewPublishedPayload {
return {
review_id: review.id,
company_id: review.companyId,
change,
rating: review.rating,
company_review_count: stats.reviewCount,
company_rating_sum: stats.ratingSum,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class ReviewModerationService {
type: REVIEW_PUBLISHED,
aggregateId: review.companyId,
correlationId,
payload: buildReviewPublished(review, stats, at),
payload: buildReviewPublished(review, stats, direction === 1 ? 'PUBLISHED' : 'REMOVED', at),
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions apps/lifecycle/test/db/reviews.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ describe('Lifecycle 1.8 — รีวิวบริษัท (HTTP + Postgres

const events = await publishedEvents();
expect(events).to.have.length(2);
expect(events[1]).to.include({ review_id: b.id, company_id: companyId, rating: 2, company_review_count: 2, company_rating_sum: 6, entity_version: 2 });
expect(events[1]).to.include({ review_id: b.id, company_id: companyId, change: 'PUBLISHED', rating: 2, company_review_count: 2, company_rating_sum: 6, entity_version: 2 });

const history = await prisma.statusHistory.findMany({ where: { entity: 'REVIEW', entityId: a.id }, orderBy: { occurredAt: 'asc' } });
expect(history.map((h) => [h.fromStatus, h.toStatus, h.actorRole])).to.deep.equal([
Expand Down Expand Up @@ -314,7 +314,7 @@ describe('Lifecycle 1.8 — รีวิวบริษัท (HTTP + Postgres
expect(await reviewStatus(a.id)).to.equal('REMOVED');
expect(await stats()).to.include({ reviewCount: 1, ratingSum: 3, entityVersion: 3 });
const last = (await publishedEvents()).at(-1)!;
expect(last).to.include({ review_id: a.id, company_review_count: 1, company_rating_sum: 3, entity_version: 3 });
expect(last).to.include({ review_id: a.id, change: 'REMOVED', company_review_count: 1, company_rating_sum: 3, entity_version: 3 });

const c = await submitted();
await deliver(c.id, 'HOLD');
Expand All @@ -332,7 +332,7 @@ describe('Lifecycle 1.8 — รีวิวบริษัท (HTTP + Postgres
await deliver(r.id, 'ADMIN_REMOVE');
expect(await reviewStatus(r.id)).to.equal('REMOVED');
expect(await stats()).to.include({ reviewCount: 0, ratingSum: 0 });
expect((await publishedEvents()).at(-1)).to.include({ review_id: r.id, company_review_count: 0, company_rating_sum: 0 });
expect((await publishedEvents()).at(-1)).to.include({ review_id: r.id, change: 'REMOVED', company_review_count: 0, company_rating_sum: 0 });
});

it('สถานะไม่ตรง ⇒ ข้ามไม่ throw (ack): ADMIN_PUBLISH ของ PUBLISHED, PUBLISH/HOLD ของ HELD/REMOVED, ADMIN_REMOVE ซ้ำ, ไม่พบรีวิว', async () => {
Expand Down
5 changes: 5 additions & 0 deletions libs/contracts/src/events/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export interface ReviewSubmittedPayload {
export interface ReviewPublishedPayload {
review_id: string;
company_id: string;
/**
* `PUBLISHED` = รีวิวเพิ่งเผยแพร่; `REMOVED` = Admin ถอดรีวิวที่เผยแพร่แล้ว (`ADMIN_REMOVE`) — PM ตัดสิน 2026-11-22
* ผู้รับที่ได้ event ไม่มีฟิลด์นี้ (ส่งก่อนเพิ่ม) ให้ถือเป็น `PUBLISHED`
*/
change: 'PUBLISHED' | 'REMOVED';
rating: number;
/** จำนวนรีวิวที่เผยแพร่รวมของบริษัท หลังรวมรีวิวนี้ */
company_review_count: number;
Expand Down
Loading