mirror of
https://github.com/hex248/sprint.git
synced 2026-02-07 18:23:03 +00:00
98 lines
3.0 KiB
TypeScript
98 lines
3.0 KiB
TypeScript
import { Organisation, OrganisationMember } from "@issue/shared";
|
|
import { and, eq } from "drizzle-orm";
|
|
import { db } from "../client";
|
|
|
|
export async function createOrganisation(name: string, slug: string, description?: string) {
|
|
const [organisation] = await db
|
|
.insert(Organisation)
|
|
.values({
|
|
name,
|
|
slug,
|
|
description,
|
|
})
|
|
.returning();
|
|
return organisation;
|
|
}
|
|
|
|
export async function createOrganisationMember(
|
|
organisationId: number,
|
|
userId: number,
|
|
role: string = "member",
|
|
) {
|
|
const [member] = await db
|
|
.insert(OrganisationMember)
|
|
.values({
|
|
organisationId,
|
|
userId,
|
|
role,
|
|
})
|
|
.returning();
|
|
return member;
|
|
}
|
|
|
|
export async function getOrganisationById(id: number) {
|
|
const [organisation] = await db.select().from(Organisation).where(eq(Organisation.id, id));
|
|
return organisation;
|
|
}
|
|
|
|
export async function getOrganisationBySlug(slug: string) {
|
|
const [organisation] = await db.select().from(Organisation).where(eq(Organisation.slug, slug));
|
|
return organisation;
|
|
}
|
|
|
|
export async function getOrganisationsByUserId(userId: number) {
|
|
const organisations = await db
|
|
.select()
|
|
.from(OrganisationMember)
|
|
.where(eq(OrganisationMember.userId, userId))
|
|
.innerJoin(Organisation, eq(OrganisationMember.organisationId, Organisation.id));
|
|
return organisations;
|
|
}
|
|
|
|
export async function updateOrganisation(
|
|
organisationId: number,
|
|
updates: { name?: string; description?: string; slug?: string },
|
|
) {
|
|
const [organisation] = await db
|
|
.update(Organisation)
|
|
.set(updates)
|
|
.where(eq(Organisation.id, organisationId))
|
|
.returning();
|
|
return organisation;
|
|
}
|
|
|
|
export async function deleteOrganisation(organisationId: number) {
|
|
// Delete all organisation members first
|
|
await db.delete(OrganisationMember).where(eq(OrganisationMember.organisationId, organisationId));
|
|
// Delete the organisation
|
|
await db.delete(Organisation).where(eq(Organisation.id, organisationId));
|
|
}
|
|
|
|
export async function getOrganisationMembers(organisationId: number) {
|
|
const members = await db
|
|
.select()
|
|
.from(OrganisationMember)
|
|
.where(eq(OrganisationMember.organisationId, organisationId))
|
|
.innerJoin(Organisation, eq(OrganisationMember.organisationId, Organisation.id));
|
|
return members;
|
|
}
|
|
|
|
export async function removeOrganisationMember(organisationId: number, userId: number) {
|
|
await db
|
|
.delete(OrganisationMember)
|
|
.where(
|
|
and(eq(OrganisationMember.organisationId, organisationId), eq(OrganisationMember.userId, userId)),
|
|
);
|
|
}
|
|
|
|
export async function updateOrganisationMemberRole(organisationId: number, userId: number, role: string) {
|
|
const [member] = await db
|
|
.update(OrganisationMember)
|
|
.set({ role })
|
|
.where(
|
|
and(eq(OrganisationMember.organisationId, organisationId), eq(OrganisationMember.userId, userId)),
|
|
)
|
|
.returning();
|
|
return member;
|
|
}
|