diff --git a/snapshots/collections-schema.yml b/snapshots/collections-schema.yml index c213141..f0f784e 100644 --- a/snapshots/collections-schema.yml +++ b/snapshots/collections-schema.yml @@ -322,7 +322,7 @@ fields: options: null readonly: false required: false - sort: 19 + sort: 20 special: null translations: null validation: null @@ -638,7 +638,7 @@ fields: label: Is Custom City readonly: false required: false - sort: 21 + sort: 22 special: - cast-boolean translations: null @@ -717,7 +717,7 @@ fields: options: null readonly: false required: false - sort: 17 + sort: 18 special: null translations: null validation: null @@ -755,7 +755,7 @@ fields: options: null readonly: false required: false - sort: 18 + sort: 19 special: null translations: null validation: null @@ -833,7 +833,7 @@ fields: options: null readonly: false required: false - sort: 20 + sort: 21 special: null translations: null validation: null @@ -871,7 +871,7 @@ fields: options: null readonly: false required: false - sort: 22 + sort: 23 special: null translations: null validation: null @@ -947,7 +947,7 @@ fields: options: null readonly: false required: true - sort: 16 + sort: 17 special: null translations: null validation: null @@ -1145,7 +1145,7 @@ fields: options: null readonly: false required: false - sort: 15 + sort: 16 special: null translations: null validation: null @@ -1167,6 +1167,44 @@ fields: has_auto_increment: false foreign_key_table: null foreign_key_column: null + - collection: gp_adopted_probes + field: nodeVersion + type: string + meta: + collection: gp_adopted_probes + conditions: null + display: null + display_options: null + field: nodeVersion + group: null + hidden: false + interface: input + note: null + options: null + readonly: false + required: false + sort: 15 + special: null + translations: null + validation: null + validation_message: null + width: full + schema: + name: nodeVersion + table: gp_adopted_probes + data_type: varchar + default_value: null + max_length: 255 + numeric_precision: null + numeric_scale: null + is_nullable: true + is_unique: false + is_primary_key: false + is_generated: false + generation_expression: null + has_auto_increment: false + foreign_key_table: null + foreign_key_column: null - collection: gp_credits field: id type: integer diff --git a/src/extensions/endpoints/adoption-code/src/index.ts b/src/extensions/endpoints/adoption-code/src/index.ts index aa43ada..2c02277 100644 --- a/src/extensions/endpoints/adoption-code/src/index.ts +++ b/src/extensions/endpoints/adoption-code/src/index.ts @@ -16,6 +16,7 @@ type Request = ExpressRequest & { type SendCodeResponse = { uuid: string; version: string; + nodeVersion: string; hardwareDevice: string | null; status: string; city: string; @@ -32,6 +33,7 @@ type AdoptedProbe = { code: string; uuid: string | null; version: string | null; + nodeVersion: string | null; hardwareDevice: string | null; status: string; city: string | null; @@ -90,6 +92,7 @@ export default defineEndpoint((router, { env, logger, services }) => { code, uuid: null, version: null, + nodeVersion: null, hardwareDevice: null, status: 'offline', city: null, @@ -113,6 +116,7 @@ export default defineEndpoint((router, { env, logger, services }) => { code, uuid: response.data.uuid, version: response.data.version, + nodeVersion: response.data.nodeVersion, hardwareDevice: response.data.hardwareDevice || null, status: response.data.status, city: response.data.city, @@ -174,6 +178,7 @@ export default defineEndpoint((router, { env, logger, services }) => { ip: probe.ip, uuid: probe.uuid, version: probe.version, + nodeVersion: probe.nodeVersion, hardwareDevice: probe.hardwareDevice, status: probe.status, city: probe.city, diff --git a/src/extensions/endpoints/adoption-code/test/index.test.ts b/src/extensions/endpoints/adoption-code/test/index.test.ts index 2c486b8..07b0ef3 100644 --- a/src/extensions/endpoints/adoption-code/test/index.test.ts +++ b/src/extensions/endpoints/adoption-code/test/index.test.ts @@ -67,6 +67,7 @@ describe('adoption code endpoints', () => { }).reply(200, { uuid: '35cadbfd-2079-4b1f-a4e6-5d220035132a', version: '0.26.0', + nodeVersion: '18.17.0', hardwareDevice: null, status: 'ready', city: 'Paris', @@ -159,6 +160,7 @@ describe('adoption code endpoints', () => { }).reply(200, { uuid: '35cadbfd-2079-4b1f-a4e6-5d220035132a', version: '0.26.0', + nodeVersion: '18.17.0', hardwareDevice: 'v1', status: 'ready', city: 'Paris', @@ -197,6 +199,7 @@ describe('adoption code endpoints', () => { ip: '1.1.1.1', uuid: '35cadbfd-2079-4b1f-a4e6-5d220035132a', version: '0.26.0', + nodeVersion: '18.17.0', hardwareDevice: 'v1', status: 'ready', city: 'Paris', @@ -249,6 +252,7 @@ describe('adoption code endpoints', () => { ip: '1.1.1.1', uuid: null, version: null, + nodeVersion: null, hardwareDevice: null, status: 'offline', city: null, @@ -274,6 +278,7 @@ describe('adoption code endpoints', () => { }).reply(200, { uuid: '35cadbfd-2079-4b1f-a4e6-5d220035132a', version: '0.26.0', + nodeVersion: '18.17.0', hardwareDevice: null, status: 'ready', city: 'Paris', @@ -312,6 +317,7 @@ describe('adoption code endpoints', () => { ip: '1.1.1.1', uuid: '35cadbfd-2079-4b1f-a4e6-5d220035132a', version: '0.26.0', + nodeVersion: '18.17.0', hardwareDevice: null, status: 'ready', city: 'Paris', @@ -337,6 +343,7 @@ describe('adoption code endpoints', () => { }).reply(200, { uuid: '35cadbfd-2079-4b1f-a4e6-5d220035132a', version: '0.26.0', + nodeVersion: '18.17.0', hardwareDevice: null, status: 'ready', city: 'Paris', @@ -379,6 +386,7 @@ describe('adoption code endpoints', () => { }).reply(200, { uuid: '35cadbfd-2079-4b1f-a4e6-5d220035132a', version: '0.26.0', + nodeVersion: '18.17.0', hardwareDevice: null, status: 'ready', city: 'Paris', @@ -422,6 +430,7 @@ describe('adoption code endpoints', () => { }).reply(200, { uuid: '35cadbfd-2079-4b1f-a4e6-5d220035132a', version: '0.26.0', + nodeVersion: '18.17.0', hardwareDevice: null, status: 'ready', city: 'Paris', diff --git a/src/extensions/migrations/20240423A-add-permissions-to-read-adopted-node-version.js b/src/extensions/migrations/20240423A-add-permissions-to-read-adopted-node-version.js new file mode 100644 index 0000000..c87b158 --- /dev/null +++ b/src/extensions/migrations/20240423A-add-permissions-to-read-adopted-node-version.js @@ -0,0 +1,71 @@ +const DIRECTUS_URL = process.env.DIRECTUS_URL; +const ADMIN_ACCESS_TOKEN = process.env.ADMIN_ACCESS_TOKEN; +const USER_ROLE_NAME = 'User'; + +const COLLECTION_NAME = 'gp_adopted_probes'; +const FIELDS_TO_REMOVE = []; +const FIELDS_TO_ADD = [ 'nodeVersion' ]; + +async function getUserRoleId () { + const URL = `${DIRECTUS_URL}/roles?filter[name][_eq]=${USER_ROLE_NAME}&access_token=${ADMIN_ACCESS_TOKEN}`; + const response = await fetch(URL).then((response) => { + if (!response.ok) { + throw new Error(`Fetch request failed. Status: ${response.status}`); + } + + return response.json(); + }); + return response.data[0].id; +} + +async function getUserPermissions (roleId) { + const URL = `${DIRECTUS_URL}/permissions?filter[collection][_eq]=${COLLECTION_NAME}&filter[role][_eq]=${roleId}&access_token=${ADMIN_ACCESS_TOKEN}`; + const response = await fetch(URL).then((response) => { + if (!response.ok) { + throw new Error(`Fetch request failed. Status: ${response.status}`); + } + + return response.json(); + }); + const permissions = response.data; + const readPermissions = permissions.find(({ action }) => action === 'read'); + + return { readPermissions }; +} + +async function patchReadPermissions (readPermissions) { + const URL = `${DIRECTUS_URL}/permissions/${readPermissions.id}?access_token=${ADMIN_ACCESS_TOKEN}`; + const filteredFields = readPermissions.fields.filter(field => !FIELDS_TO_REMOVE.includes(field)); + + const response = await fetch(URL, { + method: 'PATCH', + body: JSON.stringify({ + ...readPermissions, + fields: [ + ...filteredFields, + ...FIELDS_TO_ADD, + ], + }), + headers: { + 'Content-Type': 'application/json', + }, + }).then((response) => { + if (!response.ok) { + throw new Error(`Fetch request failed. Status: ${response.status}`); + } + + return response.json(); + }); + return response.data; +} + +export async function up () { + const roleId = await getUserRoleId(); + const { readPermissions } = await getUserPermissions(roleId); + await patchReadPermissions(readPermissions); + console.log('User permissions patched read adopted probes "nodeVersion" field'); +} + +export async function down () { + console.log('There is no down operation for that migration.'); +}