mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-31 06:59:33 +02:00
refactor create and update resource endpoints
This commit is contained in:
parent
e41c0c9d7c
commit
6f96598ad4
3 changed files with 543 additions and 311 deletions
|
@ -2,7 +2,15 @@ import { Request, Response, NextFunction } from "express";
|
|||
import { z } from "zod";
|
||||
import { db } from "@server/db";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { Org, orgs, roleActions, roles, userOrgs } from "@server/db/schema";
|
||||
import {
|
||||
domains,
|
||||
Org,
|
||||
orgDomains,
|
||||
orgs,
|
||||
roleActions,
|
||||
roles,
|
||||
userOrgs
|
||||
} from "@server/db/schema";
|
||||
import response from "@server/lib/response";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
import createHttpError from "http-errors";
|
||||
|
@ -16,7 +24,6 @@ const createOrgSchema = z
|
|||
.object({
|
||||
orgId: z.string(),
|
||||
name: z.string().min(1).max(255)
|
||||
// domain: z.string().min(1).max(255).optional(),
|
||||
})
|
||||
.strict();
|
||||
|
||||
|
@ -82,14 +89,13 @@ export async function createOrg(
|
|||
let org: Org | null = null;
|
||||
|
||||
await db.transaction(async (trx) => {
|
||||
const domain = config.getBaseDomain();
|
||||
const allDomains = await trx.select().from(domains);
|
||||
|
||||
const newOrg = await trx
|
||||
.insert(orgs)
|
||||
.values({
|
||||
orgId,
|
||||
name,
|
||||
domain
|
||||
name
|
||||
})
|
||||
.returning();
|
||||
|
||||
|
@ -109,6 +115,13 @@ export async function createOrg(
|
|||
return;
|
||||
}
|
||||
|
||||
await trx.insert(orgDomains).values(
|
||||
allDomains.map((domain) => ({
|
||||
orgId: newOrg[0].orgId,
|
||||
domainId: domain.domainId
|
||||
}))
|
||||
);
|
||||
|
||||
await trx.insert(userOrgs).values({
|
||||
userId: req.user!.userId,
|
||||
orgId: newOrg[0].orgId,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue