Skip to content

When swapping directors, keep members active that are filling other roles #3501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,20 @@ export class ProjectMemberGelRepository
),
),
}));
const inactivated = e.update(members, () => ({
const inactivated = e.update(members, (member) => ({
filter: e.op(e.count(member.roles), '=', 1),
set: {
inactiveAt: e.datetime_of_transaction(),
},
}));
const replacements = e.for(inactivated.project, (project) =>
const fillingOtherRoles = e.update(members, (member) => ({
filter: e.op(e.count(member.roles), '>', 1),
set: {
roles: { '-=': $.role },
},
}));
const projects = e.op(inactivated, 'union', fillingOtherRoles).project;
const replacements = e.for(projects, (project) =>
e
.insert(e.Project.Member, {
project,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
variable,
} from '~/core/database/query';
import { type FilterFn } from '~/core/database/query/filters';
import { conditionalOn } from '~/core/database/query/properties/update-property';
import { userFilters, UserRepository } from '../../user/user.repository';
import { type ProjectFilters } from '../dto';
import { projectFilters } from '../project-filters.query';
Expand Down Expand Up @@ -235,15 +236,47 @@ export class ProjectMemberRepository extends DtoRepository(ProjectMember) {
project: { status: ['Active', 'InDevelopment'] },
}),
)
.apply(
updateProperty({
resource: ProjectMember,
key: 'inactiveAt',
value: now,
permanentAfter: 0,
}),
.subQuery('node', (sub) =>
sub
.match([
node('node'),
relation('out', '', 'roles', ACTIVE),
node('roles', 'Property'),
])
.apply(
conditionalOn(
'size(roles.value) > 1',
['node'],
// If there are other roles, remove this role from the membership & keep it active
(q) =>
q
.apply(
updateProperty({
resource: ProjectMember,
key: 'roles',
value: variable(
apoc.coll.disjunction('roles.value', [`"${role}"`]),
),
permanentAfter: 0,
}),
)
.return('stats'),
// Else then mark the membership inactive & maintain the role
(q) =>
q
.apply(
updateProperty({
resource: ProjectMember,
key: 'inactiveAt',
value: now,
permanentAfter: 0,
}),
)
.return('stats'),
),
)
.return('stats as oldMemberStats'),
)
.with('project')
.subQuery('project', (sub) =>
sub
.match([
Expand Down
5 changes: 5 additions & 0 deletions src/core/database/query/cypher-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ export const apoc = {
coll: {
flatten: fn1('apoc.coll.flatten'),
indexOf: fn('apoc.coll.indexOf'),
/**
* Returns the items in the first list that aren't in the second list.
* @see https://neo4j.com/docs/apoc/current/overview/apoc.coll/apoc.coll.disjunction/
*/
disjunction: fn2('apoc.coll.disjunction'),
/**
* Returns the distinct union of the two given LIST<ANY> values.
* @see https://neo4j.com/docs/apoc/current/overview/apoc.coll/apoc.coll.union/
Expand Down
2 changes: 1 addition & 1 deletion src/core/database/query/properties/update-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export const conditionalOn = <R>(
trueQuery: QueryFragment<unknown, R>,
falseQuery: QueryFragment<unknown, R>,
): QueryFragment<unknown, R> => {
const imports = [...new Set([conditionVar, ...scope])];
const imports = [...new Set([varInExp(conditionVar), ...scope])];
return (query) =>
query.subQuery((sub) =>
sub
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { entries, mapEntries } from '@seedcompany/common';
import { DateTime } from 'luxon';
import { type ID, Role } from '~/common';
import { graphql } from '~/graphql';
Expand Down Expand Up @@ -66,6 +67,15 @@ it('director change replaces memberships on open projects', async () => {
});
return project;
})(),
hasMemberIncludingOtherRoles: await (async () => {
const project = await createProject(app);
await createProjectMember(app, {
projectId: project.id,
userId: directors.old.id,
roles: [Role.RegionalDirector, Role.ProjectManager],
});
return project;
})(),
alreadyHasRoleFilled: await (async () => {
const project = await createProject(app);
await createProjectMember(app, {
Expand Down Expand Up @@ -138,33 +148,13 @@ it('director change replaces memberships on open projects', async () => {
};

const getResults = async () => {
const results = {
needsSwapA: await fetchMembers(app, projects.needsSwapA.id),
needsSwapB: await fetchMembers(app, projects.needsSwapB.id),
doesNotHaveMember: await fetchMembers(app, projects.doesNotHaveMember.id),
hasMemberButInactive: await fetchMembers(
app,
projects.hasMemberButInactive.id,
),
alreadyHasRoleFilled: await fetchMembers(
app,
projects.alreadyHasRoleFilled.id,
),
alreadyHasNewDirectorActive: await fetchMembers(
app,
projects.alreadyHasNewDirectorActive.id,
),
alreadyHasNewDirectorInactive: await fetchMembers(
app,
projects.alreadyHasNewDirectorInactive.id,
),
alreadyHasNewDirectorWithoutRole: await fetchMembers(
app,
projects.alreadyHasNewDirectorWithoutRole.id,
),
closed: await fetchMembers(app, projects.closed.id),
};

const resultList = await Promise.all(
entries(projects).map(async ([key, project]) => {
const results = await fetchMembers(app, project.id);
return [key, results] as const;
}),
);
const results = mapEntries(resultList, (i) => i).asRecord;
return {
get: (project: keyof typeof results, key: keyof typeof directors) => {
const member = results[project].find(
Expand Down Expand Up @@ -200,6 +190,14 @@ it('director change replaces memberships on open projects', async () => {
expect(before.get('hasMemberButInactive', 'old')).toEqual(InactiveRD);
expect(before.get('hasMemberButInactive', 'new')).toBeUndefined();
expect(before.get('hasMemberButInactive', 'unrelated')).toBeUndefined();
expect(before.get('hasMemberIncludingOtherRoles', 'old')).toEqual({
active: true,
roles: [Role.RegionalDirector, Role.ProjectManager],
});
expect(before.get('hasMemberIncludingOtherRoles', 'new')).toBeUndefined();
expect(
before.get('hasMemberIncludingOtherRoles', 'unrelated'),
).toBeUndefined();
expect(before.get('alreadyHasRoleFilled', 'old')).toBeUndefined();
expect(before.get('alreadyHasRoleFilled', 'new')).toBeUndefined();
expect(before.get('alreadyHasRoleFilled', 'unrelated')).toEqual(ActiveRD);
Expand Down Expand Up @@ -264,6 +262,14 @@ it('director change replaces memberships on open projects', async () => {
expect(after.get('hasMemberButInactive', 'old')).toEqual(InactiveRD);
expect(after.get('hasMemberButInactive', 'new')).toBeUndefined();
expect(after.get('hasMemberButInactive', 'unrelated')).toBeUndefined();
expect(after.get('hasMemberIncludingOtherRoles', 'old')).toEqual({
active: true, // still active
roles: [Role.ProjectManager], // but without RD
});
expect(after.get('hasMemberIncludingOtherRoles', 'new')).toEqual(ActiveRD);
expect(
after.get('hasMemberIncludingOtherRoles', 'unrelated'),
).toBeUndefined();
expect(after.get('alreadyHasRoleFilled', 'old')).toBeUndefined();
expect(after.get('alreadyHasRoleFilled', 'new')).toBeUndefined();
expect(after.get('alreadyHasRoleFilled', 'unrelated')).toEqual(ActiveRD);
Expand Down