/
get-alert-options-from-deep.ts
43 lines (36 loc) · 1.49 KB
/
get-alert-options-from-deep.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
import { AlertOptions } from "@capacitor/dialog";
import { DeepClient } from "@deep-foundation/deeplinks/imports/client";
import { DIALOG_PACKAGE_NAME } from "./package-name";
export async function getAlertOptionsFromDeep({ deep, alertLinkId }: { deep: DeepClient, alertLinkId: number }): Promise<AlertOptions> {
const titleTypeLinkId = await deep.id(DIALOG_PACKAGE_NAME, "AlertTitle");
const messageTypeLinkId = await deep.id(DIALOG_PACKAGE_NAME, "AlertMessage");
const buttonTitleTypeLinkId = await deep.id(DIALOG_PACKAGE_NAME, "AlertButtonTitle");
const dialogTreeLinkId = await deep.id(DIALOG_PACKAGE_NAME, "DialogTree")
const { data: linksDownToAlertMp } = await deep.select({
up: {
parent_id: { _eq: alertLinkId },
tree_id: { _eq: dialogTreeLinkId }
}
},
{
returning: `type_id id from_id to_id
to {
value
}
`
});
const titleLink = linksDownToAlertMp.find(link => link.type_id === titleTypeLinkId);
if(!titleLink) {
throw new Error(`A link with type ##${titleTypeLinkId} is not found`)
}
const linkWithMessage = linksDownToAlertMp.find(link => link.type_id === messageTypeLinkId);
if(!linkWithMessage) {
throw new Error(`A link with type ##${messageTypeLinkId} is not found`)
}
const linkWithButtonTitle = linksDownToAlertMp.find(link => link.type_id === buttonTitleTypeLinkId);
return {
title: titleLink.to.value.value,
message: linkWithMessage.to.value.value,
buttonTitle: linkWithButtonTitle?.to.value.value
}
}