Skip to content

Commit

Permalink
[ASAP-853] Enable Hub Admin assign user role to a Lab in CMS (#4516)
Browse files Browse the repository at this point in the history
* creates labPi field in labs & renames labs (labsMembership) field to oldLabs & creates new labs field (as lab)

* script to transfer data from labMembership to labs

* fixes linting issues

* adds workflow to run data rollback script (assign users as PIs)

* alligns gql and data structure with current users-labs rollback

* takes previous labPi when assigning PI to a lab

* fixes name of labPI field in migration

* fixes labs reference

* optimizes sent requests on labPi assignement
  • Loading branch information
AimeurAmin authored Feb 12, 2025
1 parent 1ce6259 commit a159732
Show file tree
Hide file tree
Showing 18 changed files with 674 additions and 226 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/on-demand-migrate-user-labs-rollback.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Migrate user labs rollback
on:
workflow_dispatch:
inputs:
environment-name:
required: true
description: The environment name
type: choice
options:
- Branch
- Development
- Production
contentful-env:
required: true
type: string
description: Choose the Contentful environment to migrate user labs

jobs:
migrate_user_labs_rollback:
runs-on: ubuntu-latest
environment: ${{ inputs.environment-name }}
container:
image: ghcr.io/yldio/asap-hub/node-python-sq:7b77d99657ab3718ed548ba366ffbcbb15315fd8
credentials:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup
id: setup
uses: ./.github/actions/setup-environment
with:
environment-name: ${{ inputs.environment-name }}
- name: Run User Assignment as Lab PI Migration
run: |
yarn workspace @asap-hub/contentful migrate-assign-users-as-lab-pi
env:
CONTENTFUL_MANAGEMENT_ACCESS_TOKEN: ${{ secrets.CONTENTFUL_MANAGEMENT_TOKEN }}
CONTENTFUL_SPACE_ID: ${{ steps.setup.outputs.crn-contentful-space-id }}
CONTENTFUL_ENV_ID: ${{ inputs.contentful-env }}

notify_failure:
runs-on: ubuntu-latest
needs: [migrate_user_labs_rollback]
if: ${{ failure() && inputs.environment-name=='Production' }}
steps:
- name: Checkout
uses: actions/checkout@v3
- uses: ./.github/actions/slack/
with:
message: Migrate user labs rollback failed
webhook: ${{ secrets.SLACK_WEBHOOK }}
status: failure
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export const parseContentfulGraphQlTeamListItem = (
membership.linkedFrom?.usersCollection?.items[0] || {};

const memberLabIds = labsCollection?.items
.map((labMembership) => labMembership?.lab?.sys.id)
.map((labItem) => labItem?.sys.id)
.filter((x): x is string => x !== undefined);

memberLabIds?.forEach((labId) => labIdsSet.add(labId));
Expand Down Expand Up @@ -281,16 +281,16 @@ export const parseContentfulGraphQlTeam = (
} = membership.linkedFrom?.usersCollection?.items[0] || {};

const labs: LabResponse[] = (labsCollection?.items || []).reduce(
(userLabs: LabResponse[], labMembership): LabResponse[] => {
if (!labMembership || !labMembership.lab) {
(userLabs: LabResponse[], lab): LabResponse[] => {
if (!lab) {
return userLabs;
}

return [
...userLabs,
{
id: labMembership.lab.sys.id,
name: labMembership.lab.name || '',
id: lab.sys.id,
name: lab.name || '',
},
];
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,16 +548,16 @@ export const parseLabsCollection = (
labsCollection: LabsCollection,
): LabResponse[] =>
(labsCollection?.items || []).reduce(
(userLabs: LabResponse[], labMembership): LabResponse[] => {
if (!labMembership || !labMembership.lab?.name) {
(userLabs: LabResponse[], lab): LabResponse[] => {
if (!lab || !lab.name) {
return userLabs;
}

return [
...userLabs,
{
id: labMembership.lab.sys.id,
name: labMembership.lab.name,
id: lab.sys.id,
name: lab.name,
},
];
},
Expand Down
5 changes: 2 additions & 3 deletions apps/crn-server/src/utils/analytics/collaboration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ export const getUserDataById = (
teamItem?.inactiveSinceDate ?? undefined,
})) || [],
labIds:
item?.labsCollection?.items.map(
(labMembership) => labMembership?.lab?.sys.id || '',
) || [],
item?.labsCollection?.items.map((labItem) => labItem?.sys.id || '') ||
[],
teamIds:
item?.teamsCollection?.items.map(
(teamItem) => teamItem?.team?.sys.id || '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,8 @@ describe('Teams data provider', () => {
...getContentfulGraphqlTeamMembers(),
labsCollection: {
items: [
{ lab: { sys: { id: 'lab-1' } } },
{ lab: { sys: { id: 'lab-2' } } },
{ sys: { id: 'lab-1' } },
{ sys: { id: 'lab-2' } },
],
},
},
Expand Down Expand Up @@ -439,7 +439,7 @@ describe('Teams data provider', () => {
{
...getContentfulGraphqlTeamMembers(),
labsCollection: {
items: [null, { lab: { sys: { id: 'lab-2' } } }],
items: [null, { sys: { id: 'lab-2' } }],
},
},
],
Expand Down Expand Up @@ -483,9 +483,9 @@ describe('Teams data provider', () => {
...getContentfulGraphqlTeamMembers(),
labsCollection: {
items: [
{ lab: { sys: { id: 'lab-1' } } },
{ lab: { sys: { id: 'lab-2' } } },
{ lab: { sys: { id: 'lab-3' } } },
{ sys: { id: 'lab-1' } },
{ sys: { id: 'lab-2' } },
{ sys: { id: 'lab-3' } },
],
},
},
Expand All @@ -504,9 +504,9 @@ describe('Teams data provider', () => {
...getContentfulGraphqlTeamMembers(),
labsCollection: {
items: [
{ lab: { sys: { id: 'lab-1' } } },
{ lab: { sys: { id: 'lab-2' } } },
{ lab: { sys: { id: 'lab-4' } } },
{ sys: { id: 'lab-1' } },
{ sys: { id: 'lab-2' } },
{ sys: { id: 'lab-4' } },
],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,20 +228,16 @@ describe('User data provider', () => {
labsCollection: {
items: [
{
lab: {
sys: {
id: '1',
},
name: 'My lab',
sys: {
id: '1',
},
name: 'My lab',
},
{
lab: {
sys: {
id: '2',
},
name: null,
sys: {
id: '2',
},
name: null,
},
],
},
Expand Down
6 changes: 2 additions & 4 deletions apps/crn-server/test/fixtures/analytics.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,8 @@ export const getUserTotalResearchOutputsItem = ({
alumniSinceDate: null,
labsCollection: {
items: labIds.map((labId) => ({
lab: {
sys: {
id: labId,
},
sys: {
id: labId,
},
})),
},
Expand Down
20 changes: 3 additions & 17 deletions apps/crn-server/test/fixtures/teams.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,24 +153,10 @@ export const getContentfulGraphqlTeamMembers = () => ({
onboarded: true,
});

type LabsCollectionType = NonNullable<
NonNullable<
NonNullable<
NonNullable<
NonNullable<
NonNullable<
NonNullable<FetchTeamByIdQuery['teams']>['linkedFrom']
>['teamMembershipCollection']
>['items'][number]
>['linkedFrom']
>['usersCollection']
>['items'][number]
>['labsCollection'];

export const getContentfulGraphqlTeamMemberLabs = (): LabsCollectionType => ({
export const getContentfulGraphqlTeamMemberLabs = () => ({
items: [
{ lab: { sys: { id: 'cd7be4902' }, name: 'Brighton' } },
{ lab: { sys: { id: 'cd7be4903' }, name: 'Liverpool' } },
{ sys: { id: 'cd7be4902' }, name: 'Brighton' },
{ sys: { id: 'cd7be4903' }, name: 'Liverpool' },
],
});

Expand Down
8 changes: 4 additions & 4 deletions apps/crn-server/test/fixtures/users.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,8 @@ export const getContentfulGraphqlUser = (
role: 'Grantee',
labsCollection: {
items: [
{ lab: { sys: { id: 'cd7be4902' }, name: 'Brighton' } },
{ lab: { sys: { id: 'cd7be4903' }, name: 'Liverpool' } },
{ sys: { id: 'cd7be4902' }, name: 'Brighton' },
{ sys: { id: 'cd7be4903' }, name: 'Liverpool' },
],
},
teamsCollection: {
Expand Down Expand Up @@ -581,8 +581,8 @@ export const getContentfulGraphqlUserListItem = (
role: 'Grantee',
labsCollection: {
items: [
{ lab: { sys: { id: 'cd7be4902' }, name: 'Brighton' } },
{ lab: { sys: { id: 'cd7be4903' }, name: 'Liverpool' } },
{ sys: { id: 'cd7be4902' }, name: 'Brighton' },
{ sys: { id: 'cd7be4903' }, name: 'Liverpool' },
],
},
teamsCollection: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module.exports.description =
'Renames lab membership reference (labs to oldLabs) and creates new labs field (references labs) adds PI field';

module.exports.up = (migration) => {
const labs = migration.editContentType('labs');

labs
.createField('labPI')
.name('Principle Investigator')
.type('Link')
.linkType('Entry')
.validations([
{
linkContentType: ['users'],
},
]);

const users = migration.editContentType('users');

users.changeFieldId('labs', 'oldLabs');
users.editField('oldLabs', { name: 'Old Labs' });
users
.createField('labs')
.name('Labs')
.type('Array')
.items({
type: 'Link',
linkType: 'Entry',
validations: [
{
linkContentType: ['labs'],
},
],
});
users.moveField('labs').afterField('teams');
};

module.exports.down = (migration) => {
const labs = migration.editContentType('labs');
labs.deleteField('labPI');

const users = migration.editContentType('users');
users.deleteField('labs');
users.changeFieldId('oldLabs', 'labs');
users.editField('oldLabs', { name: 'Labs' });
users.moveField('labs').afterField('teams');
};
1 change: 1 addition & 0 deletions packages/contentful/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"scripts": {
"build:babel": "../../scripts/build-babel.sh build",
"migrate-user-labs": "ts-node ./scripts/migrate-user-labs.ts",
"migrate-assign-users-as-lab-pi": "ts-node ./scripts/assign-users-as-lab-pi.ts",
"migrate-preprint-versions": "ts-node ./scripts/migrate-research-output-versions.ts",
"watch:babel": "../../scripts/build-babel.sh watch",
"space:rollback-migration": "yarn ctf-migrate down -c $CONTENT_TYPE -d $DRY_RUN",
Expand Down
Loading

0 comments on commit a159732

Please sign in to comment.