|
1 | 1 | import type { IApp } from '@plumber/types' |
2 | 2 |
|
3 | | -import aisayApp from '@/apps/aisay' |
4 | | -import calculatorApp from '@/apps/calculator' |
5 | | -import customApiApp from '@/apps/custom-api' |
6 | | -import delayApp from '@/apps/delay' |
7 | | -import formatterApp from '@/apps/formatter' |
8 | | -import formsgApp from '@/apps/formsg/' |
9 | | -import gathersgApp from '@/apps/gathersg' |
10 | | -import lettersgApp from '@/apps/lettersg' |
11 | | -import m365ExcelApp from '@/apps/m365-excel' |
12 | | -import paysgApp from '@/apps/paysg' |
13 | | -import postmanApp from '@/apps/postman' |
14 | | -import postmanSmsApp from '@/apps/postman-sms' |
15 | | -import schedulerApp from '@/apps/scheduler' |
16 | | -import slackApp from '@/apps/slack' |
17 | | -import telegramBotApp from '@/apps/telegram-bot' |
18 | | -import tilesApp from '@/apps/tiles' |
19 | | -import toolboxApp from '@/apps/toolbox' |
20 | | -import twilioApp from '@/apps/twilio' |
21 | | -import vaultWorkspaceApp from '@/apps/vault-workspace' |
22 | | -import webhookApp from '@/apps/webhook' |
| 3 | +import { memoize } from 'lodash' |
| 4 | + |
| 5 | +import { ACTION_APPS_RANKING, TRIGGER_APPS_RANKING } from '@/apps' |
23 | 6 | import App from '@/models/app' |
24 | 7 |
|
25 | 8 | import type { QueryResolvers } from '../__generated__/types.generated' |
26 | 9 |
|
27 | | -/** |
28 | | - * Note: Remember to add the priority of the app here whenever a new app |
29 | | - * is created, this is to determine which app dropdown option appears first. |
30 | | - * Note that triggers and actions have separate rankings! |
31 | | - * Triggers: formsg, scheduler, webhook |
32 | | - * Actions: email by postman, tiles, m365, toolbox, formatter, calculator, delay, |
33 | | - * paysg, lettersg, sms by postman, telegram, slack, custom-api, vault, twilio |
34 | | - */ |
35 | | - |
36 | | -export const TRIGGER_APPS_RANKING = [ |
37 | | - formsgApp.key, |
38 | | - schedulerApp.key, |
39 | | - webhookApp.key, |
40 | | -] |
41 | | -export const ACTION_APPS_RANKING = [ |
42 | | - postmanApp.key, |
43 | | - tilesApp.key, |
44 | | - m365ExcelApp.key, |
45 | | - toolboxApp.key, |
46 | | - formatterApp.key, |
47 | | - calculatorApp.key, |
48 | | - delayApp.key, |
49 | | - paysgApp.key, |
50 | | - lettersgApp.key, |
51 | | - postmanSmsApp.key, |
52 | | - telegramBotApp.key, |
53 | | - slackApp.key, |
54 | | - aisayApp.key, |
55 | | - gathersgApp.key, |
56 | | - customApiApp.key, |
57 | | - vaultWorkspaceApp.key, |
58 | | - twilioApp.key, |
59 | | -] |
60 | | - |
61 | | -function sortApps(apps: IApp[]): IApp[] { |
62 | | - // trade off for increased time complexity but easier to add a new app to the ranking |
63 | | - return apps.sort((a, b) => { |
64 | | - const firstPriority = a.triggers |
65 | | - ? TRIGGER_APPS_RANKING.findIndex((app) => app === a.key) |
66 | | - : ACTION_APPS_RANKING.findIndex((app) => app === a.key) |
67 | | - const secondPriority = b.triggers |
68 | | - ? TRIGGER_APPS_RANKING.findIndex((app) => app === b.key) |
69 | | - : ACTION_APPS_RANKING.findIndex((app) => app === b.key) |
70 | | - |
71 | | - // sort by newApp flag, followed by priority |
72 | | - if (a.isNewApp && b.isNewApp) { |
| 10 | +const getSortedApps = memoize( |
| 11 | + async (): Promise<IApp[]> => { |
| 12 | + const apps = await App.findAll() |
| 13 | + |
| 14 | + console.log('no cache hit') |
| 15 | + // trade off for increased time complexity but easier to add a new app to the ranking |
| 16 | + return apps.sort((a, b) => { |
| 17 | + const firstPriority = a.triggers |
| 18 | + ? TRIGGER_APPS_RANKING.findIndex((app) => app === a.key) |
| 19 | + : ACTION_APPS_RANKING.findIndex((app) => app === a.key) |
| 20 | + const secondPriority = b.triggers |
| 21 | + ? TRIGGER_APPS_RANKING.findIndex((app) => app === b.key) |
| 22 | + : ACTION_APPS_RANKING.findIndex((app) => app === b.key) |
| 23 | + |
| 24 | + // sort by newApp flag, followed by priority |
| 25 | + if (a.isNewApp && b.isNewApp) { |
| 26 | + return firstPriority - secondPriority |
| 27 | + } |
| 28 | + if (a.isNewApp) { |
| 29 | + return -1 |
| 30 | + } |
| 31 | + if (b.isNewApp) { |
| 32 | + return 1 |
| 33 | + } |
73 | 34 | return firstPriority - secondPriority |
74 | | - } |
75 | | - if (a.isNewApp) { |
76 | | - return -1 |
77 | | - } |
78 | | - if (b.isNewApp) { |
79 | | - return 1 |
80 | | - } |
81 | | - return firstPriority - secondPriority |
82 | | - }) |
83 | | -} |
84 | | - |
85 | | -const getApps: QueryResolvers['getApps'] = async (_parent, params) => { |
86 | | - const apps = sortApps(await App.findAll(params.name)) |
87 | | - if (params.onlyWithTriggers) { |
88 | | - return apps.filter((app) => app.triggers?.length) |
89 | | - } |
90 | | - |
91 | | - if (params.onlyWithActions) { |
92 | | - return apps.filter((app) => app.actions?.length) |
93 | | - } |
94 | | - |
| 35 | + }) |
| 36 | + }, |
| 37 | + () => 'getApps', |
| 38 | +) |
| 39 | + |
| 40 | +const getApps: QueryResolvers['getApps'] = async () => { |
| 41 | + console.time('getApps') |
| 42 | + const apps = await getSortedApps() |
| 43 | + console.timeEnd('getApps') |
95 | 44 | return apps |
96 | 45 | } |
97 | 46 |
|
|
0 commit comments