Skip to content

Commit

Permalink
Set should_be_managed_by__release & supervisor service_installs on de…
Browse files Browse the repository at this point in the history
…vice POST

Change-type: minor
  • Loading branch information
thgreasi committed Dec 12, 2023
1 parent d6799b8 commit 76090ec
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/features/supervisor-app/hooks/supervisor-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,40 @@ import type {

const { BadRequestError } = pinejsErrors;

hooks.addPureHook('POST', 'resin', 'device', {
async POSTPARSE({ request, api }) {
if (
request.values.supervisor_version != null &&
request.values.is_of__device_type != null
) {
const deviceType = (await api.get({
resource: 'device_type',
id: request.values.is_of__device_type,
options: {
$select: 'is_of__cpu_architecture',
},
})) as PickDeferred<DeviceType, 'is_of__cpu_architecture'> | null;
if (deviceType == null) {
return;
}

const [supervisorRelease] = await getSupervisorReleaseResource(
api,
request.values.supervisor_version,
deviceType.is_of__cpu_architecture.__id,
);

if (supervisorRelease == null) {
return;
}

// since this is a POST, we _know_ the device is being created and has no current/target state, so we can
// just append the target after determining which it is (like a preloaded app)
request.values.should_be_managed_by__release = supervisorRelease.id;
}
},
});

hooks.addPureHook('PATCH', 'resin', 'device', {
/**
* When a device checks in with it's initial supervisor version, set the corresponding should_be_managed_by__release resource
Expand Down Expand Up @@ -134,7 +168,7 @@ async function checkSupervisorReleaseUpgrades(
async function getSupervisorReleaseResource(
api: sbvrUtils.PinejsClient,
supervisorVersion: string,
archId: string,
archId: string | number,
) {
return (await api.get({
resource: 'release',
Expand Down

0 comments on commit 76090ec

Please sign in to comment.