-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(members): add by email (#553)
* ✨ feat: add member by email * ♻️ refactor: use union response for success and error * chore: use member constructor in repository
- Loading branch information
Showing
20 changed files
with
617 additions
and
133 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
19 changes: 19 additions & 0 deletions
19
src/graphql/organizations/resolvers/AddMemberErrorResponse.ts
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,19 @@ | ||
import type { AddMemberErrorResponseResolvers } from "./../../types.generated.js"; | ||
export const AddMemberErrorResponse: AddMemberErrorResponseResolvers = { | ||
code: ({ error }) => { | ||
switch (error.name) { | ||
case "InvalidArgumentError": | ||
return "ALREADY_MEMBER"; | ||
case "NotFoundError": | ||
return "USER_NOT_FOUND"; | ||
} | ||
}, | ||
message: ({ error }) => { | ||
switch (error.name) { | ||
case "InvalidArgumentError": | ||
return "User is already a member of the organization"; | ||
case "NotFoundError": | ||
return "User not found"; | ||
} | ||
}, | ||
}; |
This file was deleted.
Oops, something went wrong.
2 changes: 2 additions & 0 deletions
2
src/graphql/organizations/resolvers/AddMemberSuccessResponse.ts
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,2 @@ | ||
import type { AddMemberSuccessResponseResolvers } from "./../../types.generated.js"; | ||
export const AddMemberSuccessResponse: AddMemberSuccessResponseResolvers = {}; |
56 changes: 43 additions & 13 deletions
56
src/graphql/organizations/resolvers/Mutation/addMember.ts
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 |
---|---|---|
@@ -1,23 +1,53 @@ | ||
import { ApolloServerErrorCode } from "@apollo/server/errors"; | ||
import { GraphQLError } from "graphql"; | ||
import { OrganizationRole } from "~/domain/organizations.js"; | ||
import type { MutationResolvers } from "./../../../types.generated.js"; | ||
export const addMember: NonNullable<MutationResolvers["addMember"]> = async ( | ||
_parent, | ||
{ data }, | ||
ctx, | ||
) => { | ||
const { userId: memberId, organizationId, role } = data; | ||
const addMemberResult = await ctx.organizations.members.addMember(ctx, { | ||
userId: memberId, | ||
organizationId, | ||
role: role ?? OrganizationRole.MEMBER, | ||
}); | ||
if (!addMemberResult.ok) { | ||
switch (addMemberResult.error.name) { | ||
case "PermissionDeniedError": | ||
case "UnauthorizedError": | ||
throw addMemberResult.error; | ||
const { userId, email, organizationId, role } = data; | ||
let addMemberResult: Awaited< | ||
ReturnType<typeof ctx.organizations.members.addMember> | ||
>; | ||
if (userId) { | ||
addMemberResult = await ctx.organizations.members.addMember(ctx, { | ||
userId, | ||
organizationId, | ||
role: role ?? OrganizationRole.MEMBER, | ||
}); | ||
} else if (email) { | ||
addMemberResult = await ctx.organizations.members.addMember(ctx, { | ||
email, | ||
organizationId, | ||
role: role ?? OrganizationRole.MEMBER, | ||
}); | ||
} else { | ||
throw new GraphQLError("You must provide either a userId or an email.", { | ||
extensions: { | ||
code: ApolloServerErrorCode.BAD_USER_INPUT, | ||
}, | ||
}); | ||
} | ||
|
||
if (addMemberResult.ok) { | ||
return { | ||
__typename: "AddMemberSuccessResponse", | ||
member: addMemberResult.data.member, | ||
}; | ||
} | ||
|
||
switch (addMemberResult.error.name) { | ||
case "InvalidArgumentError": | ||
case "NotFoundError": { | ||
return { | ||
__typename: "AddMemberErrorResponse", | ||
error: addMemberResult.error, | ||
}; | ||
} | ||
default: { | ||
throw addMemberResult.error; | ||
} | ||
} | ||
const { member } = addMemberResult.data; | ||
return { member }; | ||
}; |
Oops, something went wrong.