Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
🐛 Can;t invite member who is already there
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed May 12, 2019
1 parent 2f59e2f commit e4e27e6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
36 changes: 31 additions & 5 deletions src/crud/membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { getOrganization } from "./organization";
import { ErrorCode, CacheCategories } from "../interfaces/enum";
import { deleteItemFromCache, cachedQuery } from "../helpers/cache";
import { getUser } from "./user";
import { Organization } from "../interfaces/tables/organization";

/*
* Create a new organization membership for a user
Expand Down Expand Up @@ -127,11 +128,8 @@ export const getOrganizationMemberDetails = async (organizationId: number) => {

export const getUserMemberships = async (user: User | number) => {
if (typeof user !== "number" && typeof user !== "string") {
if (user.id) {
user = user.id;
} else {
throw new Error(ErrorCode.USER_NOT_FOUND);
}
if (user.id) user = user.id;
else throw new Error(ErrorCode.USER_NOT_FOUND);
}
return <Membership[]>(
await cachedQuery(
Expand All @@ -143,10 +141,38 @@ export const getUserMemberships = async (user: User | number) => {
);
};

/**
* Get a detailed object of a user's membership
*/
export const getUserMembershipsDetailed = async (user: User | number) => {
const memberships: any = await getUserMemberships(user);
for await (const membership of memberships) {
membership.organization = await getOrganization(membership.organizationId);
}
return memberships;
};

/**
* Get a user membership of a particular organization
*/
export const getUserOrganizationMembership = async (
user: User | number,
organization: Organization | number
) => {
if (typeof user !== "number" && typeof user !== "string") {
if (user.id) user = user.id;
else throw new Error(ErrorCode.USER_NOT_FOUND);
}
if (typeof organization !== "number" && typeof organization !== "string") {
if (organization.id) organization = organization.id;
else throw new Error(ErrorCode.ORGANIZATION_NOT_FOUND);
}
return (<Membership[]>(
await cachedQuery(
CacheCategories.USER_MEMBERSHIP_ORGANIZATION,
`${user}_${organization}`,
`SELECT * FROM memberships WHERE userId = ? AND organizationId = ?`,
[user, organization]
)
))[0];
};
4 changes: 3 additions & 1 deletion src/interfaces/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export enum ErrorCode {
VALIDATION_EMAIL = "400/validation-email",
VALIDATION_PHONE = "400/validation-phone",
VALIDATION_URL = "400/validation-url",
CANNOT_DELETE_SOLE_OWNER = "400/cannot-delete-sole-owner"
CANNOT_DELETE_SOLE_OWNER = "400/cannot-delete-sole-owner",
USER_IS_MEMBER_ALREADY = "400/user-is-member-already"
}

export enum Templates {
Expand All @@ -88,6 +89,7 @@ export enum CacheCategories {
USER_EVENT = "user-event",
USER_RECENT_EVENTS = "user-recent-events",
USER_MEMBERSHIPS = "user-memberships",
USER_MEMBERSHIP_ORGANIZATION = "user-membership-org",
ORGANIZATION_MEMBERSHIPS = "memberships",
MEMBERSHIP = "membership",
ORGANIZATION = "organization",
Expand Down
9 changes: 8 additions & 1 deletion src/rest/membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
createMembership,
getMembership,
deleteMembership,
getOrganizationMembers
getOrganizationMembers,
getUserOrganizationMembership
} from "../crud/membership";
import { User } from "../interfaces/tables/user";
import { register } from "./auth";
Expand Down Expand Up @@ -55,6 +56,12 @@ export const inviteMemberToOrganization = async (
if (userExists) {
newUser = await getUserByEmail(newMemberEmail);
if (!newUser.id) throw new Error(ErrorCode.USER_NOT_FOUND);
let isMemberAlready = false;
try {
await getUserOrganizationMembership(newUser.id, organizationId);
isMemberAlready = true;
} catch (error) {}
if (isMemberAlready) throw new Error(ErrorCode.USER_IS_MEMBER_ALREADY);
await createMembership({ userId: newUser.id, organizationId, role });
return;
} else {
Expand Down

0 comments on commit e4e27e6

Please sign in to comment.