summaryrefslogtreecommitdiff
path: root/lib/model.ts
blob: 7896d70d7b0aa6ef2cc3f6727559fe1d24ccc3bf (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
import { isObject } from '@emprespresso/pengueno';

export interface Email {
    from: string;
    to: string;
    subject: string;
    text: string;
}

export interface EmailCredentials {
    email: string;
    username: string;
    password: string;
}

export const isCreds = (u: unknown): u is EmailCredentials =>
    !!(
        isObject(u) &&
        ['email', 'username', 'password'].every((key) => key in u && typeof u[key as keyof typeof u] === 'string')
    );

export interface EmailSendInstruction extends EmailCredentials {
    send_host: string;
    send_port: number;
}

export const isSend = (u: unknown): u is EmailSendInstruction =>
    !!(
        isCreds(u) &&
        'send_host' in u &&
        typeof u.send_host === 'string' &&
        'send_port' in u &&
        typeof u.send_port === 'number'
    );

export interface EmailRecvInstruction extends EmailCredentials {
    read_host: string;
    read_port: number;
}

export const isRecv = (u: unknown): u is EmailSendInstruction =>
    !!(
        isCreds(u) &&
        'read_host' in u &&
        typeof u.read_host === 'string' &&
        'read_port' in u &&
        typeof u.read_port === 'number'
    );

export interface EmailJob {
    from: EmailSendInstruction;
    to: EmailRecvInstruction;
    readRetry: Retry;
}

export const isEmailJob = (u: unknown): u is EmailJob =>
    !!(
        isObject(u) &&
        'from' in u &&
        isSend(u.from) &&
        'to' in u &&
        isRecv(u.to) &&
        'readRetry' in u &&
        isRetry(u.readRetry)
    );

export interface Retry {
    attempts: number;
    interval: number;
}

export const isRetry = (u: unknown): u is Retry =>
    !!(
        isObject(u) && ['attempts', 'interval'].every((key) => key in u && typeof u[key as keyof typeof u] === 'number')
    );

export const redact = <T extends EmailCredentials>(instruction: T): T => ({
    ...instruction,
    password: 'REDACTED',
    username: 'REDACTED',
});

export const redactJob = (job: EmailJob): EmailJob => ({
    ...job,
    from: redact(job.from),
    to: redact(job.to),
});