/
context.ts
59 lines (57 loc) · 1.69 KB
/
context.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
import { EVENT_TYPES, TemplateRegistry, UserCsvRow } from "./types";
import { differenceInYears } from "date-fns";
import { withOrdinal } from "./fns";
/**
* Register your context handlers
* specific to event type here
*/
export const contextHandlers = {
[EVENT_TYPES.BIRTHDAY]: (template: TemplateRegistry, user: UserCsvRow) => {
const attachments = [...template.attachments];
const imageUrl = "cid:" + (attachments.pop()?.cid || "");
return {
userName: user.name,
imageUrl,
};
},
[EVENT_TYPES.ANNIVERSARY]: (
template: TemplateRegistry,
user: UserCsvRow
) => {
const attachments = [...template.attachments];
const imageUrl = "cid:" + (attachments.pop()?.cid || "");
const joiningDate = new Date(user.joining_date);
const numberOfYears = differenceInYears(new Date(), joiningDate);
const numberOfYearsWithOrdinal = withOrdinal(numberOfYears);
return {
userName: user.name,
imageUrl,
numberOfYears,
numberOfYearsWithOrdinal,
};
},
};
/**
* This function will be called to
* retrieve the context of a specific template.
*
*
* Dont override this one unless you know what
* you are doing. if you want to add some
* more data to some specific template context.
* then do it in `contextHandlers` object under
* their specific event.
*
* @param template
* @param user
*/
export const pleaseGetContext = (
template: TemplateRegistry,
user: UserCsvRow
) => {
const contextHandler = contextHandlers[template.id];
if (!contextHandler) {
return {};
}
return contextHandler(template, user);
};