aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main/src/stores/Service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/main/src/stores/Service.ts')
-rw-r--r--packages/main/src/stores/Service.ts63
1 files changed, 63 insertions, 0 deletions
diff --git a/packages/main/src/stores/Service.ts b/packages/main/src/stores/Service.ts
new file mode 100644
index 0000000..9bc6a43
--- /dev/null
+++ b/packages/main/src/stores/Service.ts
@@ -0,0 +1,63 @@
1/*
2 * Copyright (C) 2022 Kristóf Marussy <kristof@marussy.com>
3 *
4 * This file is part of Sophie.
5 *
6 * Sophie is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, version 3.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: AGPL-3.0-only
19 */
20
21import type { ServiceSnapshotIn } from '@sophie/shared';
22
23import generateId from '../utils/generateId';
24
25import type { ProfileSnapshotIn } from './Profile';
26
27export interface PartialServiceSnapshotIn
28 extends Omit<ServiceSnapshotIn, 'id' | 'profile'> {
29 id?: string | undefined;
30 profile?: string | undefined;
31}
32
33export function addMissingServiceIdsAndProfiles(
34 partialServices: PartialServiceSnapshotIn[] | undefined,
35 profiles: ProfileSnapshotIn[],
36): ServiceSnapshotIn[] {
37 return (partialServices ?? []).map((service) => {
38 const { name } = service;
39 let { id, profile } = service;
40 if (typeof id === 'undefined') {
41 id = generateId(name);
42 }
43 if (typeof profile === 'undefined') {
44 profile = generateId(name);
45 profiles.push({
46 id: profile,
47 name: service.name,
48 });
49 }
50 return {
51 ...service,
52 id,
53 profile,
54 };
55 });
56}
57
58export type {
59 Service,
60 ServiceSnapshotOut,
61 ServiceSnapshotIn,
62} from '@sophie/shared';
63export { service } from '@sophie/shared';