-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sync adopter probe node version with gp
- Loading branch information
1 parent
9a94ef3
commit b5abb6f
Showing
4 changed files
with
131 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/extensions/migrations/20240423A-add-permissions-to-read-adopted-node-version.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.'); | ||
} |