Skip to content

Commit 27013a9

Browse files
committed
Add Translation WebHooks
1 parent a2ff894 commit 27013a9

7 files changed

Lines changed: 54 additions & 7 deletions

File tree

functions/src/models/webhook.model.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export enum WebHookEvent {
1616
CONTENT_UNPUBLISHED = 'content.unpublished',
1717
CONTENT_DELETED = 'content.deleted',
1818
CONTENT_UPDATED = 'content.updated',
19+
TRANSLATION_PUBLISHED = 'translation.published',
20+
TRANSLATION_ADDED = 'translation.added',
21+
TRANSLATION_UPDATED = 'translation.updated',
22+
TRANSLATION_DELETED = 'translation.deleted',
1923
}
2024

2125
export type WebHookStatus = 'success' | 'failure';
@@ -24,14 +28,18 @@ export interface WebHookPayload {
2428
event: WebHookEvent;
2529
spaceId: string;
2630
timestamp: string;
27-
data: ContentWebHookPayloadData;
31+
data: ContentWebHookPayloadData | TranslationWebHookPayloadData;
2832
signature?: string;
2933
}
3034

3135
export interface ContentWebHookPayloadData {
3236
contentId: string;
3337
}
3438

39+
export interface TranslationWebHookPayloadData {
40+
translationId?: string;
41+
}
42+
3543
export interface WebHookLog {
3644
event: WebHookEvent;
3745
status: WebHookStatus;

functions/src/translations.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
import { deleteTranslations, findSpaceById, findTranslations, findTranslationsHistory, spaceTranslationCachePath } from './services';
1616
import { translateCloud, translateWithGoogle } from './services/translate.service';
1717
import { canPerform } from './utils/user-auth-utils';
18+
import { triggerWebHooksForEvent } from './utils/webhook-utils';
19+
import { WebHookEvent, WebHookPayload } from './models/webhook.model';
1820

1921
// Publish
2022
const publish = onCall<PublishTranslationsData>(async request => {
@@ -59,6 +61,13 @@ const publish = onCall<PublishTranslationsData>(async request => {
5961
createdAt: FieldValue.serverTimestamp(),
6062
};
6163
await findTranslationsHistory(spaceId).add(addHistory);
64+
const webhookPayload: WebHookPayload = {
65+
event: WebHookEvent.TRANSLATION_PUBLISHED,
66+
spaceId,
67+
timestamp: new Date().toISOString(),
68+
data: {},
69+
};
70+
await triggerWebHooksForEvent(spaceId, webhookPayload);
6271
return;
6372
} else {
6473
logger.info(`[translationsPublish] Space ${spaceId} does not exist or no translations.`);
@@ -257,6 +266,26 @@ const onWriteToHistory = onDocumentWritten('spaces/{spaceId}/translations/{trans
257266
addHistory.name = afterData.updatedBy.name;
258267
}
259268
await findTranslationsHistory(spaceId).add(addHistory);
269+
270+
// Trigger webhook based on operation type
271+
let webhookEvent: WebHookEvent | undefined;
272+
if (beforeData && afterData) {
273+
webhookEvent = WebHookEvent.TRANSLATION_UPDATED;
274+
} else if (beforeData) {
275+
webhookEvent = WebHookEvent.TRANSLATION_DELETED;
276+
} else if (afterData) {
277+
webhookEvent = WebHookEvent.TRANSLATION_ADDED;
278+
}
279+
if (webhookEvent) {
280+
const webhookPayload: WebHookPayload = {
281+
event: webhookEvent,
282+
spaceId,
283+
timestamp: new Date().toISOString(),
284+
data: { translationId },
285+
};
286+
await triggerWebHooksForEvent(spaceId, webhookPayload);
287+
}
288+
260289
const countSnapshot = await findTranslationsHistory(spaceId).count().get();
261290
const { count } = countSnapshot.data();
262291
if (count > 30) {

src/app/features/spaces/contents/edit-document/edit-document.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ export class EditDocumentComponent implements OnInit, DirtyFormGuardComponent {
272272
constructor() {}
273273

274274
ngOnInit(): void {
275-
this.history$ = this.contentHistoryService.findAll(this.spaceId(), this.contentId()).pipe(takeUntilDestroyed(this.destroyRef));
275+
this.history$ = this.contentHistoryService.findAll(this.spaceId(), this.contentId(), 30).pipe(takeUntilDestroyed(this.destroyRef));
276276
const document = this.document();
277277
// Initialize document data
278278
if (document.kind === ContentKind.DOCUMENT) {

src/app/features/spaces/translations/translations.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ export class TranslationsComponent implements OnInit {
326326
this.isLoading.set(false);
327327
},
328328
});
329-
this.history$ = this.translateHistoryService.findAll(this.spaceId()).pipe(takeUntilDestroyed(this.destroyRef));
329+
this.history$ = this.translateHistoryService.findAll(this.spaceId(), 30).pipe(takeUntilDestroyed(this.destroyRef));
330330
}
331331

332332
publish(): void {

src/app/shared/models/webhook.model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ export enum WebHookEvent {
1717
CONTENT_UNPUBLISHED = 'content.unpublished',
1818
CONTENT_DELETED = 'content.deleted',
1919
CONTENT_UPDATED = 'content.updated',
20+
TRANSLATION_PUBLISHED = 'translation.published',
21+
TRANSLATION_ADDED = 'translation.added',
22+
TRANSLATION_UPDATED = 'translation.updated',
23+
TRANSLATION_DELETED = 'translation.deleted',
2024
}
2125

2226
export type WebHookStatus = 'success' | 'failure';

src/app/shared/services/content-history.service.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ import { map } from 'rxjs/operators';
99
export class ContentHistoryService {
1010
private readonly firestore = inject(Firestore);
1111

12-
findAll(spaceId: string, id: string): Observable<ContentHistory[]> {
13-
const queryConstrains: QueryConstraint[] = [orderBy('createdAt', 'desc'), limit(30)];
12+
findAll(spaceId: string, id: string, max?: number): Observable<ContentHistory[]> {
13+
const queryConstrains: QueryConstraint[] = [orderBy('createdAt', 'desc')];
14+
if (max) {
15+
queryConstrains.push(limit(max));
16+
}
1417
return collectionData(query(collection(this.firestore, `spaces/${spaceId}/contents/${id}/histories`), ...queryConstrains), {
1518
idField: 'id',
1619
}).pipe(

src/app/shared/services/translation-history.service.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ import { map } from 'rxjs/operators';
99
export class TranslationHistoryService {
1010
private readonly firestore = inject(Firestore);
1111

12-
findAll(spaceId: string): Observable<TranslationHistory[]> {
13-
const queryConstrains: QueryConstraint[] = [orderBy('createdAt', 'desc'), limit(30)];
12+
findAll(spaceId: string, max?: number): Observable<TranslationHistory[]> {
13+
const queryConstrains: QueryConstraint[] = [orderBy('createdAt', 'desc')];
14+
if (max) {
15+
queryConstrains.push(limit(max));
16+
}
1417
return collectionData(query(collection(this.firestore, `spaces/${spaceId}/translations_history`), ...queryConstrains), {
1518
idField: 'id',
1619
}).pipe(

0 commit comments

Comments
 (0)