Skip to content

Commit

Permalink
fix: update can-edit-event to work in cases where initiative is delet…
Browse files Browse the repository at this point in the history
…ed (#1474)
  • Loading branch information
juliannemarik authored Apr 17, 2024
1 parent a886e9d commit a07470b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
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

0 comments on commit a07470b

Please sign in to comment.