Skip to content
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

fix: update can-edit-event to work in cases where initiative is deleted #1474

Merged
merged 3 commits into from
Apr 17, 2024
Merged
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
10 changes: 5 additions & 5 deletions packages/common/src/access/can-edit-event.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { IUser } from "@esri/arcgis-rest-types";
import { IItem, IUser } from "@esri/arcgis-rest-types";
import { IInitiativeModel } from "../types";
import { getProp } from "../objects";
import { findBy } from "../util";
import { hasBasePriv } from "./has-base-priv";

export interface IEventModel {
initiative: IInitiativeModel;
site: IItem;
[propName: string]: any;
}

Expand All @@ -18,10 +19,9 @@ export interface IEventModel {
export function canEditEvent(model: IEventModel, user: IUser): boolean {
let res = false;
if (hasBasePriv(user)) {
const coreTeamId = getProp(
model,
"initiative.item.properties.collaborationGroupId"
);
const coreTeamId = model.initiative
? getProp(model, "initiative.item.properties.collaborationGroupId")
: getProp(model, "site.properties.collaborationGroupId");
const { groups = [] } = user;
res = !!coreTeamId && !!findBy(groups, "id", coreTeamId);
}
Expand Down
32 changes: 26 additions & 6 deletions packages/common/test/access/can-edit-event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { canEditEvent, IEventModel } from "../../src/access/can-edit-event";
import * as baseUtils from "../../src/access/has-base-priv";
import { IModel } from "../../src/types";

describe("canEditEvent", function() {
describe("canEditEvent", function () {
const getModel = (collaborationGroupId: any): IEventModel => {
const item = {
properties: {
collaborationGroupId
}
collaborationGroupId,
},
} as IItem;
const initiative = { item } as IModel;
return { initiative } as IEventModel;
Expand All @@ -26,7 +26,7 @@ describe("canEditEvent", function() {
hasBasePrivSpy.calls.reset();
});

it(`returns true when user has base priv and is member of event's related initiative collab group`, function() {
it(`returns true when user has base priv and is member of event's related initiative collab group`, function () {
const groupId = "foo";
const model = getModel(groupId);
const group = getGroup({ id: groupId });
Expand All @@ -37,7 +37,20 @@ describe("canEditEvent", function() {
expect(hasBasePrivSpy.calls.argsFor(0)).toEqual([user]);
});

it(`returns false when user has base priv and is not member of event's related initiative collab group`, function() {
it(`returns true when user has base priv and is member of event's related site collab group`, function () {
const groupId = "foo";
const model = {
site: { properties: { collaborationGroupId: groupId } },
} as IEventModel;
const group = getGroup({ id: groupId });
const user = getUser({ groups: [group] });
const res = canEditEvent(model, user);
expect(res).toBe(true);
expect(hasBasePrivSpy.calls.count()).toBe(1);
expect(hasBasePrivSpy.calls.argsFor(0)).toEqual([user]);
});

it(`returns false when user has base priv and is not member of event's related initiative/site collab group`, function () {
const groupId = "foo";
const model = getModel(groupId);
const user = getUser({});
Expand All @@ -47,7 +60,14 @@ describe("canEditEvent", function() {
expect(hasBasePrivSpy.calls.argsFor(0)).toEqual([user]);
});

it(`returns false when user lacks base priv`, function() {
it("returns false if there is no collaborationGroupId", function () {
const model = {} as IEventModel;
const user = getUser({});
const res = canEditEvent(model, user);
expect(res).toBe(false);
});

it(`returns false when user lacks base priv`, function () {
hasBasePrivSpy.and.returnValue(false);
const groupId = "foo";
const model = getModel(groupId);
Expand Down
Loading