/
application-types.ts
173 lines (159 loc) · 3.8 KB
/
application-types.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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import * as _ from 'lodash';
import { sbvrUtils, errors, permissions } from '@balena/pinejs';
import * as semver from 'balena-semver';
export interface ApplicationType {
id?: number;
name: string;
slug: string;
supports_web_url: boolean;
supports_multicontainer: boolean;
supports_gateway_mode: boolean;
requires_payment: boolean;
is_legacy: boolean;
needs__os_version_range: null | string;
maximum_device_count: null | number;
description: string;
}
export const DefaultApplicationType: ApplicationType = {
name: 'Default',
slug: 'default',
supports_web_url: false,
supports_multicontainer: true,
supports_gateway_mode: true,
requires_payment: false,
is_legacy: false,
needs__os_version_range: '>=2.11.0',
maximum_device_count: null,
description:
'Default application type, allowing multiple containers and gateway mode.',
};
export class DeviceOSVersionIsTooLow extends errors.ForbiddenError {
constructor(
message = 'Device OS version is too low for the application type.',
) {
super(message);
}
}
export class WebUrlNotSupportedError extends errors.ForbiddenError {
constructor(message = 'The application type does not support web url.') {
super(message);
}
}
export const checkDevicesCanHaveDeviceURL = async (
api: sbvrUtils.PinejsClient,
deviceIDs: number[],
): Promise<void> => {
if (deviceIDs.length === 0) {
return;
}
const violators = await api.get({
resource: 'application_type',
options: {
$count: {
$top: 1,
$select: 'supports_web_url',
$filter: {
is_of__application: {
$any: {
$alias: 'a',
$expr: {
a: {
owns__device: {
$any: {
$alias: 'd',
$expr: {
d: {
id: { $in: deviceIDs },
},
},
},
},
},
},
},
},
supports_web_url: false,
},
},
},
});
if (violators > 0) {
throw new WebUrlNotSupportedError();
}
};
const getAppType = async (api: sbvrUtils.PinejsClient, appId: number) => {
const [appType] = await api.get({
resource: 'application_type',
passthrough: {
req: permissions.root,
},
options: {
$select: ['needs__os_version_range'],
$filter: {
is_of__application: {
$any: {
$alias: 'a',
$expr: {
a: {
id: appId,
},
},
},
},
},
},
});
return appType;
};
export const checkDeviceCanBeInApplication = async (
api: sbvrUtils.PinejsClient,
appId: number,
device: AnyObject,
) => {
const appType = await getAppType(api, appId);
if (appType?.needs__os_version_range == null) {
return;
}
checkVersion(device, appType);
};
export const checkDevicesCanBeInApplication = async (
api: sbvrUtils.PinejsClient,
appId: number,
deviceIds: number[],
): Promise<void> => {
const appType = await getAppType(api, appId);
if (appType?.needs__os_version_range == null) {
return;
}
const devices = await api.get({
resource: 'device',
options: {
$select: ['os_version', 'supervisor_version', 'device_name'],
$filter: {
id: { $in: deviceIds },
$or: {
os_version: { $ne: null },
supervisor_version: { $ne: null },
},
},
},
});
for (const device of devices) {
checkVersion(device, appType);
}
};
const checkVersion = (device: AnyObject, appType: AnyObject) => {
if (device.os_version == null && device.supervisor_version != null) {
throw new DeviceOSVersionIsTooLow(
`Device ${device.device_name} is too old to satisfy required version range: ${appType.needs__os_version_range}`,
);
}
if (
device.os_version != null &&
!semver.satisfies(device.os_version, appType.needs__os_version_range)
) {
throw new DeviceOSVersionIsTooLow(
`Device ${device.device_name} has OS version ${device.os_version} but needs to satisfy version range: ${appType.needs__os_version_range}`,
);
}
};