-
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.
- Loading branch information
1 parent
c3386b6
commit d4f1696
Showing
8 changed files
with
151 additions
and
18 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
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
86 changes: 86 additions & 0 deletions
86
src/extensions/migrations/20250227GP-change-the-way-adoption-is-removed.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,86 @@ | ||
import { getUserPermissions } from '../migration-utils.js'; | ||
|
||
const DIRECTUS_URL = process.env.DIRECTUS_URL; | ||
const ADMIN_ACCESS_TOKEN = process.env.ADMIN_ACCESS_TOKEN; | ||
const COLLECTION_NAME = 'gp_probes'; | ||
|
||
export async function up () { | ||
const { updatePermissions, deletePermissions } = await getUserPermissions(COLLECTION_NAME); | ||
await removeDeletePermissions(deletePermissions); | ||
await allowResettingUserId(updatePermissions); | ||
console.log(`Changed the way adoption is removed in: ${COLLECTION_NAME}`); | ||
} | ||
|
||
export async function removeDeletePermissions (permissionsObj) { | ||
if (!permissionsObj) { | ||
throw new Error(`Permissions object is empty.`); | ||
} | ||
|
||
if (!permissionsObj.id) { | ||
console.error(permissionsObj); | ||
throw new Error(`Permissions ID is missing. This may happen when there are multiple rows for the same permission type.`); | ||
} | ||
|
||
const URL = `${DIRECTUS_URL}/permissions/${permissionsObj.id}?access_token=${ADMIN_ACCESS_TOKEN}`; | ||
|
||
const response = await fetch(URL, { | ||
method: 'DELETE', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}).then((response) => { | ||
if (!response.ok) { | ||
throw new Error(`Fetch request failed. Status: ${response.status}`); | ||
} | ||
|
||
return response.text(); | ||
}); | ||
return response.data; | ||
} | ||
|
||
export async function allowResettingUserId (permissionsObj) { | ||
if (!permissionsObj) { | ||
throw new Error(`Permissions object is empty.`); | ||
} | ||
|
||
if (!permissionsObj.id) { | ||
console.error(permissionsObj); | ||
throw new Error(`Permissions ID is missing. This may happen when there are multiple rows for the same permission type.`); | ||
} | ||
|
||
const URL = `${DIRECTUS_URL}/permissions/${permissionsObj.id}?access_token=${ADMIN_ACCESS_TOKEN}`; | ||
|
||
const response = await fetch(URL, { | ||
method: 'PATCH', | ||
body: JSON.stringify({ | ||
...permissionsObj, | ||
fields: [ | ||
...permissionsObj.fields, | ||
'userId', | ||
], | ||
validation: { | ||
_and: [ | ||
{ | ||
userId: { | ||
_null: true, | ||
}, | ||
}, | ||
], | ||
}, | ||
}), | ||
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 down () { | ||
console.log('There is no down operation for that migration.'); | ||
} |
10 changes: 5 additions & 5 deletions
10
...ons/operations/remove-expired-adoptions-cron-handler/src/actions/remove-expired-probes.ts
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
6 changes: 3 additions & 3 deletions
6
src/extensions/operations/remove-expired-adoptions-cron-handler/src/api.ts
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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import type { OperationContext } from '@directus/extensions'; | ||
import { defineOperationApi } from '@directus/extensions-sdk'; | ||
import { removeExpiredProbes } from './actions/remove-expired-probes.js'; | ||
import { removeExpiredAdoptions } from './actions/remove-expired-probes.js'; | ||
|
||
export default defineOperationApi({ | ||
id: 'remove-expired-adoptions-cron-handler', | ||
handler: async (_operationData, context) => { | ||
const { removedIds, notifiedIds } = await removeExpiredProbes(context as OperationContext); | ||
const { removedIds, notifiedIds } = await removeExpiredAdoptions(context as OperationContext); | ||
|
||
return `Removed probes with ids: ${removedIds.toString() || '[]'}. Notified probes with ids: ${notifiedIds.toString() || '[]'}.`; | ||
return `Removed adoptions for probes: ${removedIds.toString() || '[]'}. Notified adoptions with ids: ${notifiedIds.toString() || '[]'}.`; | ||
}, | ||
}); |
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