feat: add logging for user and organization details in middleware and enhance organization creation with transaction support
This commit is contained in:
@@ -11,6 +11,7 @@ import { isUserOrgAdmin } from '@cio/db/queries/organization';
|
||||
export const orgAdminMiddleware = async (c: Context, next: Next) => {
|
||||
try {
|
||||
const user = c.get('user');
|
||||
console.log('orgAdminMiddleware user', user);
|
||||
if (!user) {
|
||||
return c.json(
|
||||
{
|
||||
@@ -23,6 +24,7 @@ export const orgAdminMiddleware = async (c: Context, next: Next) => {
|
||||
}
|
||||
|
||||
const orgId = c.req.header('cio-org-id');
|
||||
console.log('orgAdminMiddleware orgId', orgId);
|
||||
|
||||
if (!orgId) {
|
||||
return c.json(
|
||||
@@ -36,6 +38,7 @@ export const orgAdminMiddleware = async (c: Context, next: Next) => {
|
||||
}
|
||||
|
||||
const isAdmin = await isUserOrgAdmin(orgId, user.id);
|
||||
console.log('orgAdminMiddleware isAdmin', isAdmin);
|
||||
if (!isAdmin) {
|
||||
return c.json(
|
||||
{
|
||||
|
||||
@@ -10,6 +10,7 @@ import { getUserOrgRole } from '@cio/db/queries/organization';
|
||||
*/
|
||||
export const orgMemberMiddleware = async (c: Context, next: Next) => {
|
||||
try {
|
||||
console.log('user', c.get('user'));
|
||||
const user = c.get('user');
|
||||
if (!user) {
|
||||
return c.json(
|
||||
|
||||
@@ -10,6 +10,8 @@ import { isUserOrgTeamMember } from '@cio/db/queries/organization';
|
||||
*/
|
||||
export const orgTeamMemberMiddleware = async (c: Context, next: Next) => {
|
||||
try {
|
||||
console.log('orgTeamMemberMiddleware user', c.get('user'));
|
||||
|
||||
const user = c.get('user');
|
||||
if (!user) {
|
||||
return c.json(
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
checkSiteNameExists,
|
||||
createOrganization,
|
||||
createOrganizationMember,
|
||||
createOrganizationPlan,
|
||||
getOrganizationByProfileId,
|
||||
getOrganizationCount
|
||||
} from '@cio/db/queries';
|
||||
@@ -11,6 +12,7 @@ import { getProfileById, updateProfile } from '@cio/db/queries/auth';
|
||||
|
||||
import { env } from '@api/config/env';
|
||||
import { ROLE } from '@cio/utils/constants';
|
||||
import { PLAN } from '@cio/utils/plans';
|
||||
import { db } from '@cio/db/drizzle';
|
||||
import { sendEmail } from '@cio/email';
|
||||
|
||||
@@ -39,18 +41,40 @@ export async function createOrganizationWithOwner(
|
||||
|
||||
// Business Logic: Create org and member in a transaction
|
||||
try {
|
||||
const result = await db.transaction(async () => {
|
||||
const organization = await createOrganization({
|
||||
name: input.orgName,
|
||||
siteName: input.siteName
|
||||
});
|
||||
const result = await db.transaction(async (tx) => {
|
||||
const organization = await createOrganization(
|
||||
{
|
||||
name: input.orgName,
|
||||
siteName: input.siteName
|
||||
},
|
||||
tx
|
||||
);
|
||||
|
||||
const member = await createOrganizationMember({
|
||||
organizationId: organization.id,
|
||||
profileId,
|
||||
roleId: ROLE.ADMIN,
|
||||
verified: true
|
||||
});
|
||||
const member = await createOrganizationMember(
|
||||
{
|
||||
organizationId: organization.id,
|
||||
profileId,
|
||||
roleId: ROLE.ADMIN,
|
||||
verified: true
|
||||
},
|
||||
tx
|
||||
);
|
||||
|
||||
// Self-hosted: assign Enterprise plan to the new org
|
||||
if (env.PUBLIC_IS_SELFHOSTED === 'true') {
|
||||
await createOrganizationPlan(
|
||||
{
|
||||
orgId: organization.id,
|
||||
planName: PLAN.ENTERPRISE as 'ENTERPRISE',
|
||||
subscriptionId: `selfhosted-${organization.id}`,
|
||||
triggeredBy: member.id,
|
||||
payload: {},
|
||||
isActive: true,
|
||||
provider: 'selfhosted'
|
||||
},
|
||||
tx
|
||||
);
|
||||
}
|
||||
|
||||
return { organization, member };
|
||||
});
|
||||
|
||||
@@ -228,6 +228,7 @@ export function getApiHeaders(
|
||||
.join('; ');
|
||||
|
||||
console.log('cioCookies', cioCookies);
|
||||
console.log('cookies', cookies.getAll());
|
||||
const headers: { cookie: string; 'cio-org-id'?: string } = {
|
||||
cookie: cioCookies || ''
|
||||
};
|
||||
|
||||
@@ -11,7 +11,7 @@ import type {
|
||||
import { and, eq, inArray, or, sql } from 'drizzle-orm';
|
||||
|
||||
import { ROLE } from '@cio/utils/constants';
|
||||
import { db } from '@db/drizzle';
|
||||
import { db, type DbOrTxClient } from '@db/drizzle';
|
||||
|
||||
export function getOrgIdBySiteName(siteName: string) {
|
||||
return db.select().from(schema.organization).where(eq(schema.organization.siteName, siteName)).limit(1);
|
||||
@@ -82,14 +82,14 @@ export const getOrganizationByProfileId = async (profileId: string): Promise<Org
|
||||
}));
|
||||
};
|
||||
|
||||
export const createOrganization = async (data: TNewOrganization) => {
|
||||
const [organization] = await db.insert(schema.organization).values(data).returning();
|
||||
export const createOrganization = async (data: TNewOrganization, dbClient: DbOrTxClient = db) => {
|
||||
const [organization] = await dbClient.insert(schema.organization).values(data).returning();
|
||||
|
||||
return organization;
|
||||
};
|
||||
|
||||
export const createOrganizationMember = async (data: TNewOrganizationmember) => {
|
||||
const [member] = await db.insert(schema.organizationmember).values(data).returning();
|
||||
export const createOrganizationMember = async (data: TNewOrganizationmember, dbClient: DbOrTxClient = db) => {
|
||||
const [member] = await dbClient.insert(schema.organizationmember).values(data).returning();
|
||||
|
||||
return member;
|
||||
};
|
||||
@@ -534,10 +534,14 @@ export const getOrganizationCount = async (): Promise<number> => {
|
||||
/**
|
||||
* Creates a new organization plan
|
||||
* @param data Organization plan creation data
|
||||
* @param dbClient Optional DB or transaction client for use within transactions
|
||||
* @returns Created organization plan record
|
||||
*/
|
||||
export const createOrganizationPlan = async (data: TNewOrganizationPlan): Promise<TOrganizationPlan> => {
|
||||
const [plan] = await db.insert(schema.organizationPlan).values(data).returning();
|
||||
export const createOrganizationPlan = async (
|
||||
data: TNewOrganizationPlan,
|
||||
dbClient: DbOrTxClient = db
|
||||
): Promise<TOrganizationPlan> => {
|
||||
const [plan] = await dbClient.insert(schema.organizationPlan).values(data).returning();
|
||||
return plan;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user