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

Commit

Permalink
🐛 Invalidate cache when CRUD membershup
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed May 11, 2019
1 parent 9affe37 commit 9bf562d
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/crud/membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const createMembership = async (membership: Membership) => {
CacheCategories.ORGANIZATION_MEMBERSHIPS,
membership.organizationId
);
deleteItemFromCache(CacheCategories.USER_MEMBERSHIPS, membership.userId);
return await query(
`INSERT INTO memberships ${tableValues(membership)}`,
Object.values(membership)
Expand All @@ -34,6 +35,9 @@ export const createMembership = async (membership: Membership) => {
export const updateMembership = async (id: number, membership: KeyValue) => {
membership.updatedAt = dateToDateTime(new Date());
membership = removeReadOnlyValues(membership);
const membershipDetails = await getMembership(id);
if (membershipDetails.id)
deleteItemFromCache(CacheCategories.USER_MEMBERSHIPS, membershipDetails.id);
deleteItemFromCache(CacheCategories.MEMBERSHIP, id);
return await query(
`UPDATE memberships SET ${setValues(membership)} WHERE id = ?`,
Expand All @@ -45,6 +49,9 @@ export const updateMembership = async (id: number, membership: KeyValue) => {
* Delete an organization membership
*/
export const deleteMembership = async (id: number) => {
const membershipDetails = await getMembership(id);
if (membershipDetails.id)
deleteItemFromCache(CacheCategories.USER_MEMBERSHIPS, membershipDetails.id);
deleteItemFromCache(CacheCategories.MEMBERSHIP, id);
return await query("DELETE FROM memberships WHERE id = ?", [id]);
};
Expand All @@ -56,6 +63,11 @@ export const deleteAllOrganizationMemberships = async (
organizationId: number
) => {
deleteItemFromCache(CacheCategories.ORGANIZATION_MEMBERSHIPS, organizationId);
const allMemberships = await getOrganizationMembers(organizationId);
for await (const membership of allMemberships) {
if (membership.id)
deleteItemFromCache(CacheCategories.USER_MEMBERSHIPS, membership.id);
}
return await query("DELETE FROM memberships WHERE organizationId = ?", [
organizationId
]);
Expand All @@ -65,6 +77,11 @@ export const deleteAllOrganizationMemberships = async (
* Delete all memberships for a user
*/
export const deleteAllUserMemberships = async (userId: number) => {
const allMemberships = await getUserMemberships(userId);
for await (const membership of allMemberships) {
if (membership.id)
deleteItemFromCache(CacheCategories.USER_MEMBERSHIPS, membership.id);
}
return await query("DELETE FROM memberships WHERE userId = ?", [userId]);
};

Expand Down

0 comments on commit 9bf562d

Please sign in to comment.