-
-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: display research draft updates for author and admins (#3586)
- Loading branch information
Showing
9 changed files
with
261 additions
and
80 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { ResearchUpdateStatus, UserRole } from 'oa-shared' | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { researchUpdateStatusFilter } from './researchHelpers' | ||
|
||
import type { IResearch, IUserPPDB } from 'src/models' | ||
|
||
describe('Research Helpers', () => { | ||
describe('Research Update Status Filter', () => { | ||
it('should not show item when deleted', async () => { | ||
// prepare | ||
const user = { _id: 'author' } as IUserPPDB | ||
const item = { _createdBy: user._id } as IResearch.Item | ||
const update = { _deleted: true } as IResearch.Update | ||
|
||
// act | ||
const show = researchUpdateStatusFilter(item, update, user) | ||
|
||
// assert | ||
expect(show).toEqual(false) | ||
}) | ||
|
||
it('should not show item when deleted and draft', async () => { | ||
// prepare | ||
const user = { _id: 'author' } as IUserPPDB | ||
const item = { _createdBy: user._id } as IResearch.Item | ||
const update = { | ||
_deleted: true, | ||
status: ResearchUpdateStatus.DRAFT, | ||
} as IResearch.Update | ||
|
||
// act | ||
const show = researchUpdateStatusFilter(item, update, user) | ||
|
||
// assert | ||
expect(show).toEqual(false) | ||
}) | ||
|
||
it('should not show when draft and not author', async () => { | ||
// prepare | ||
const user = { _id: 'non-author' } as IUserPPDB | ||
const item = { _createdBy: 'author' } as IResearch.Item | ||
const update = { status: ResearchUpdateStatus.DRAFT } as IResearch.Update | ||
|
||
// act | ||
const show = researchUpdateStatusFilter(item, update, user) | ||
|
||
// assert | ||
expect(show).toEqual(false) | ||
}) | ||
|
||
it('should not show when draft and not authenticated', async () => { | ||
// prepare | ||
const user = { _id: 'author' } as IUserPPDB | ||
const item = { _createdBy: user._id } as IResearch.Item | ||
const update = { status: ResearchUpdateStatus.DRAFT } as IResearch.Update | ||
|
||
// act | ||
const show = researchUpdateStatusFilter(item, update) | ||
|
||
// assert | ||
expect(show).toEqual(false) | ||
}) | ||
|
||
it('should show when not draft and not deleted', async () => { | ||
// prepare | ||
const user = { _id: 'author' } as IUserPPDB | ||
const item = { _createdBy: user._id } as IResearch.Item | ||
const update = { | ||
status: ResearchUpdateStatus.PUBLISHED, | ||
} as IResearch.Update | ||
|
||
// act | ||
const show = researchUpdateStatusFilter(item, update) | ||
|
||
// assert | ||
expect(show).toEqual(true) | ||
}) | ||
|
||
it('should show when draft and current user is the author', async () => { | ||
// prepare | ||
const user = { _id: 'author' } as IUserPPDB | ||
const item = { _createdBy: user._id } as IResearch.Item | ||
const update = { status: ResearchUpdateStatus.DRAFT } as IResearch.Update | ||
|
||
// act | ||
const show = researchUpdateStatusFilter(item, update, user) | ||
|
||
// assert | ||
expect(show).toEqual(true) | ||
}) | ||
|
||
it('should show when draft and current user is a collaborator', async () => { | ||
// prepare | ||
const user = { _id: 'author' } as IUserPPDB | ||
const item = { collaborators: [user._id] } as IResearch.Item | ||
const update = { status: ResearchUpdateStatus.DRAFT } as IResearch.Update | ||
|
||
// act | ||
const show = researchUpdateStatusFilter(item, update, user) | ||
|
||
// assert | ||
expect(show).toEqual(true) | ||
}) | ||
|
||
it('should show when draft and current user is an Admin', async () => { | ||
// prepare | ||
const user = { _id: 'admin', userRoles: [UserRole.ADMIN] } as IUserPPDB | ||
const item = {} as IResearch.Item | ||
const update = { status: ResearchUpdateStatus.DRAFT } as IResearch.Update | ||
|
||
// act | ||
const show = researchUpdateStatusFilter(item, update, user) | ||
|
||
// assert | ||
expect(show).toEqual(true) | ||
}) | ||
}) | ||
}) |
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,47 @@ | ||
import { ResearchStatus, ResearchUpdateStatus, UserRole } from 'oa-shared' | ||
|
||
import type { IResearch, IUserPPDB } from 'src/models' | ||
|
||
export const researchUpdateStatusFilter = ( | ||
item: IResearch.Item, | ||
update: IResearch.Update, | ||
currentUser?: IUserPPDB | null, | ||
) => { | ||
const isCollaborator = | ||
currentUser?._id && | ||
item.collaborators && | ||
item.collaborators.includes(currentUser?._id) | ||
|
||
const isAuthor = item._createdBy === currentUser?._id | ||
const isAdmin = currentUser?.userRoles?.includes(UserRole.ADMIN) | ||
const isUpdateDraft = update.status === ResearchUpdateStatus.DRAFT | ||
const isUpdateDeleted = update._deleted | ||
|
||
return ( | ||
(isAdmin || isAuthor || isCollaborator || !isUpdateDraft) && | ||
!isUpdateDeleted | ||
) | ||
} | ||
|
||
export const getPublicUpdates = ( | ||
item: IResearch.Item, | ||
currentUser?: IUserPPDB | null, | ||
) => { | ||
if (item.updates) { | ||
return item.updates.filter((update) => | ||
researchUpdateStatusFilter(item, update, currentUser), | ||
) | ||
} else { | ||
return [] | ||
} | ||
} | ||
|
||
export const researchStatusColour = ( | ||
researchStatus?: ResearchStatus, | ||
): string => { | ||
return researchStatus === ResearchStatus.ARCHIVED | ||
? 'lightgrey' | ||
: researchStatus === ResearchStatus.COMPLETED | ||
? 'betaGreen' | ||
: 'accent.base' | ||
} |
Oops, something went wrong.