-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex-handler.ts
75 lines (65 loc) · 2.1 KB
/
index-handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { EventBridgeEvent } from 'aws-lambda';
import algoliasearch, { SearchClient } from 'algoliasearch';
import { TeamsEventType } from '../webhooks/webhook-teams';
import ResearchOutputs, {
ResearchOutputController,
} from '../../controllers/research-outputs';
import {
algoliaAppId,
algoliaIndexApiKey,
algoliaResearchOutputIndex,
} from '../../config';
import logger from '../../utils/logger';
export const indexResearchOutputByTeamHandler = (
researchOutputController: ResearchOutputController,
algoliaClient: SearchClient,
): ((
event: EventBridgeEvent<TeamsEventType, SquidexWebhookTeamPayload>,
) => Promise<void>) => {
const algoliaIndex = algoliaClient.initIndex(algoliaResearchOutputIndex);
return async (
event: EventBridgeEvent<TeamsEventType, SquidexWebhookTeamPayload>,
): Promise<void> => {
const outputsIds = Array.from(
new Set(
(event.detail.payload.data.outputs.iv ?? []).concat(
event.detail.payload.dataOld?.outputs.iv ?? [],
),
),
);
logger.info(`Found ${outputsIds.length} research-output(s)`);
if (outputsIds.length > 0) {
const teamOutputsResults = await Promise.allSettled(
outputsIds.map(async (id) => {
logger.debug(`Found research-output with id ${id}`);
const researchOutput = await researchOutputController.fetchById(id);
logger.debug(`Fetched ${JSON.stringify(researchOutput.id)}`);
await algoliaIndex.saveObject({
...researchOutput,
objectID: researchOutput.id,
});
logger.debug(`Saved research-output with id ${id}`);
}),
);
logger.info(JSON.stringify(teamOutputsResults));
}
};
};
export type SquidexWebhookTeamPayload = {
type: 'TeamsCreated' | 'TeamsUpdated';
payload: {
$type: 'EnrichedContentEvent';
type: 'Created';
id: string;
data: {
outputs: { iv: string[] };
};
dataOld?: {
outputs: { iv: string[] };
};
};
};
export const handler = indexResearchOutputByTeamHandler(
new ResearchOutputs(),
algoliasearch(algoliaAppId, algoliaIndexApiKey),
);