-
Notifications
You must be signed in to change notification settings - Fork 729
chore: implemented patch HTTP method for member project affiliations (CM-1041) #3912
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
Merged
+255
−35
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6db1b26
chore: implemented patch HTTP method for member project affiliations
themarolt 9a5affd
fix: fixes according to the spec and comments
themarolt c113bb0
fix: small refactor
themarolt ef8760d
fix: small refactor
themarolt ff80ae8
fix: optimization
themarolt bb4e1ea
fix: optimization
themarolt 3f27bb0
fix: bugfix on the query
themarolt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
31 changes: 31 additions & 0 deletions
31
backend/src/api/public/v1/members/project-affiliations/mappers.ts
This file contains hidden or 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,31 @@ | ||
| import type { | ||
| ISegmentAffiliationWithOrg, | ||
| IWorkExperienceAffiliation, | ||
| } from '@crowd/data-access-layer' | ||
|
|
||
| export function mapSegmentAffiliation(a: ISegmentAffiliationWithOrg) { | ||
| return { | ||
| id: a.id, | ||
| organizationId: a.organizationId, | ||
| organizationName: a.organizationName, | ||
| organizationLogo: a.organizationLogo ?? null, | ||
| verified: a.verified, | ||
| verifiedBy: a.verifiedBy ?? null, | ||
| startDate: a.dateStart ?? null, | ||
| endDate: a.dateEnd ?? null, | ||
| } | ||
| } | ||
|
|
||
| export function mapWorkExperienceAffiliation(a: IWorkExperienceAffiliation) { | ||
| return { | ||
| id: a.id, | ||
| organizationId: a.organizationId, | ||
| organizationName: a.organizationName, | ||
| organizationLogo: a.organizationLogo ?? null, | ||
| verified: a.verified ?? false, | ||
| verifiedBy: a.verifiedBy ?? null, | ||
| source: a.source ?? null, | ||
| startDate: a.dateStart ?? null, | ||
| endDate: a.dateEnd ?? null, | ||
| } | ||
| } |
125 changes: 125 additions & 0 deletions
125
backend/src/api/public/v1/members/project-affiliations/patchProjectAffiliation.ts
This file contains hidden or 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,125 @@ | ||
| import type { Request, Response } from 'express' | ||
| import { z } from 'zod' | ||
|
|
||
| import { captureApiChange, memberEditAffiliationsAction } from '@crowd/audit-logs' | ||
| import { NotFoundError } from '@crowd/common' | ||
| import { CommonMemberService } from '@crowd/common_services' | ||
| import { | ||
| MemberField, | ||
| deleteAllMemberSegmentAffiliationsForProject, | ||
| fetchMemberProjectSegments, | ||
| fetchMemberSegmentAffiliationsForProject, | ||
| findMaintainerRoles, | ||
| findMemberById, | ||
| insertMemberSegmentAffiliations, | ||
| optionsQx, | ||
| } from '@crowd/data-access-layer' | ||
| import type { ISegmentAffiliationWithOrg } from '@crowd/data-access-layer' | ||
|
|
||
| import { ok } from '@/utils/api' | ||
| import { validateOrThrow } from '@/utils/validation' | ||
|
|
||
| import { mapSegmentAffiliation } from './mappers' | ||
|
|
||
| const paramsSchema = z.object({ | ||
| memberId: z.uuid(), | ||
| projectId: z.uuid(), | ||
| }) | ||
|
|
||
| const bodySchema = z.object({ | ||
| affiliations: z | ||
| .array( | ||
| z | ||
| .object({ | ||
| organizationId: z.uuid(), | ||
| dateStart: z.coerce.date(), | ||
| dateEnd: z.coerce.date().nullable().optional(), | ||
| }) | ||
| .refine((a) => a.dateEnd == null || a.dateEnd >= a.dateStart, { | ||
| message: 'dateEnd must be greater than or equal to dateStart', | ||
| }), | ||
| ) | ||
| .min(1), | ||
| verifiedBy: z.string().max(255), | ||
| }) | ||
|
|
||
| export async function patchProjectAffiliation(req: Request, res: Response): Promise<void> { | ||
| const { memberId, projectId } = validateOrThrow(paramsSchema, req.params) | ||
| const { affiliations, verifiedBy } = validateOrThrow(bodySchema, req.body) | ||
|
|
||
| const qx = optionsQx(req) | ||
|
|
||
| const member = await findMemberById(qx, memberId, [MemberField.ID]) | ||
| if (!member) { | ||
| throw new NotFoundError('Member not found') | ||
| } | ||
|
|
||
| const [segment] = await fetchMemberProjectSegments(qx, memberId, projectId) | ||
| if (!segment) { | ||
| throw new NotFoundError('Project not found') | ||
| } | ||
|
|
||
| const existingAffiliations = await fetchMemberSegmentAffiliationsForProject( | ||
| qx, | ||
| memberId, | ||
| projectId, | ||
| ) | ||
|
|
||
| let updatedAffiliations: ISegmentAffiliationWithOrg[] = [] | ||
|
|
||
| await captureApiChange( | ||
| req, | ||
| memberEditAffiliationsAction(memberId, async (captureOldState, captureNewState) => { | ||
| captureOldState(existingAffiliations) | ||
|
|
||
| await qx.tx(async (tx) => { | ||
| await deleteAllMemberSegmentAffiliationsForProject(tx, memberId, projectId) | ||
|
|
||
| await insertMemberSegmentAffiliations( | ||
| tx, | ||
| memberId, | ||
| projectId, | ||
| affiliations.map((a) => ({ | ||
| organizationId: a.organizationId, | ||
| dateStart: a.dateStart.toISOString(), | ||
| dateEnd: a.dateEnd?.toISOString() ?? null, | ||
| verifiedBy, | ||
| })), | ||
| ) | ||
|
|
||
| const oldOrgIds = existingAffiliations.map((a) => a.organizationId) | ||
| const newOrgIds = affiliations.map((a) => a.organizationId) | ||
| const orgIdsToRecalculate = [...new Set([...oldOrgIds, ...newOrgIds])] | ||
|
|
||
| const service = new CommonMemberService(tx, req.temporal, req.log) | ||
| await service.startAffiliationRecalculation(memberId, orgIdsToRecalculate) | ||
| }) | ||
|
|
||
| updatedAffiliations = await fetchMemberSegmentAffiliationsForProject(qx, memberId, projectId) | ||
| captureNewState(updatedAffiliations) | ||
| }), | ||
| ) | ||
|
|
||
| const maintainerRoles = await findMaintainerRoles(qx, [memberId]) | ||
|
|
||
| const roles = maintainerRoles | ||
| .filter((r) => r.segmentId === projectId) | ||
| .map((r) => ({ | ||
| id: r.id, | ||
| role: r.role, | ||
| startDate: r.dateStart ?? null, | ||
| endDate: r.dateEnd ?? null, | ||
| repoUrl: r.url ?? null, | ||
| repoFileUrl: r.maintainerFile ?? null, | ||
| })) | ||
|
|
||
| ok(res, { | ||
| id: segment.id, | ||
| projectSlug: segment.slug, | ||
| projectName: segment.name, | ||
| projectLogo: segment.projectLogo ?? null, | ||
| contributionCount: Number(segment.activityCount), | ||
| roles, | ||
| affiliations: updatedAffiliations.map(mapSegmentAffiliation), | ||
| }) | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.