Skip to content

Commit

Permalink
feat: add changeOwner db function and helper
Browse files Browse the repository at this point in the history
  • Loading branch information
IanFonzie committed Apr 10, 2024
1 parent 9894261 commit b8060e5
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions src/back-end/lib/db/affiliation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
readOneOrganizationSlim
} from "back-end/lib/db/organization";
import { readOneUser } from "back-end/lib/db/user";
import { Knex } from "knex";
import { valid } from "shared/lib/http";
import {
Affiliation,
Expand Down Expand Up @@ -323,6 +324,90 @@ export const updateAdminStatus = tryDb<
}
);

// @eslint-ignore
function affiliationUpdateQuery(
connection: Connection,
rawAffiliation: Partial<RawAffiliation>,
queryObject: Record<string, Knex.MaybeRawColumn<string>>
) {
return connection<RawAffiliation>("affiliations")
.update(rawAffiliation, "*")
.where(queryObject)
.whereIn("organization", function () {
this.select("id").from("organizations").where({
active: true
});
});
}

export const changeOwner = tryDb<[Id, Id, AuthenticatedSession], Affiliation>(
async (connection, id, orgId, session: AuthenticatedSession) => {
const now = new Date();
return await connection.transaction(async (trx) => {
const [previousOwnerAffiliation] = await affiliationUpdateQuery(
connection,
{ membershipType: MembershipType.Member, updatedAt: now },
{ organization: orgId, membershipType: MembershipType.Owner }
).transacting(trx);

if (!previousOwnerAffiliation) {
throw new Error("unable to set affiliation type to member");
}

const [previousOwnerAffiliationEvent] =
await connection<RawHistoryRecord>("affiliationEvents")
.transacting(trx)
.insert(
{
id: generateUuid(),
affiliation: previousOwnerAffiliation.id,
event: AffiliationEvent.OwnerStatusRevoked,
createdAt: now,
createdBy: session.user.id
},
"*"
);

if (!previousOwnerAffiliationEvent) {
throw new Error("unable to create affiliation event");
}

const [newOwnerAffiliation] = await affiliationUpdateQuery(
connection,
{ membershipType: MembershipType.Owner, updatedAt: now },
{ id }
).transacting(trx);

if (!newOwnerAffiliation) {
throw new Error("unable to set affiliation type owner");
}

const [newOwnerAffiliationEvent] = await connection<RawHistoryRecord>(
"affiliationEvents"
)
.transacting(trx)
.insert(
{
id: generateUuid(),
affiliation: id,
event: AffiliationEvent.OwnerStatusGranted,
createdAt: now,
createdBy: session.user.id
},
"*"
);

if (!newOwnerAffiliationEvent) {
throw new Error("unable to create affiliation event");
}

return valid(
await rawAffiliationToAffiliation(connection, newOwnerAffiliation)
);
});
}
);

export const deleteAffiliation = tryDb<[Id], Affiliation>(
async (connection, id) => {
const now = new Date();
Expand Down

0 comments on commit b8060e5

Please sign in to comment.