Skip to content

Commit

Permalink
fix-rename-shortCode-shortcode (#580)
Browse files Browse the repository at this point in the history
* fix-rename-shortCode-shortcode

* fix-rename-shortCode-shortcode

* feat-spaces

* rename back
  • Loading branch information
McPizza0 authored Jul 18, 2024
1 parent f730f55 commit 7d10378
Show file tree
Hide file tree
Showing 92 changed files with 303 additions and 299 deletions.
10 changes: 5 additions & 5 deletions apps/mail-bridge/queue/mail-processor/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,14 +648,14 @@ export const worker = new Worker<
}
};

const orgShortCode = await db.query.orgs.findFirst({
const orgShortcode = await db.query.orgs.findFirst({
where: eq(orgs.id, orgId),
columns: {
shortcode: true
}
});

if (!orgShortCode) {
if (!orgShortcode) {
throw new Error('No org shortcode found');
}

Expand Down Expand Up @@ -714,7 +714,7 @@ export const worker = new Worker<
cid: attachment.cid || null
},
orgPublicId,
orgShortCode.shortcode
orgShortcode.shortcode
)
)
).then(
Expand Down Expand Up @@ -922,7 +922,7 @@ type PreSignedData = {
async function uploadAndAttachAttachment(
input: UploadAndAttachAttachmentInput,
orgPublicId: TypeId<'org'>,
orgShortCode: string
orgShortcode: string
) {
const preUpload = (await fetch(
`${env.STORAGE_URL}/api/attachments/internalPresign`,
Expand Down Expand Up @@ -976,7 +976,7 @@ async function uploadAndAttachAttachment(
});

return {
attachmentUrl: `${env.STORAGE_URL}/attachment/${orgShortCode}/${attachmentPublicId}/${input.fileName}`,
attachmentUrl: `${env.STORAGE_URL}/attachment/${orgShortcode}/${attachmentPublicId}/${input.fileName}`,
cid: input.cid,
inline: input.inline
};
Expand Down
4 changes: 2 additions & 2 deletions apps/platform/routes/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ authApi.get('/status', async (c) => {
authApi.get('/redirection', async (c) => {
const account = c.get('account');
if (!account) {
return c.json({ defaultOrgShortCode: null }, 401);
return c.json({ defaultOrgShortcode: null }, 401);
}

const accountId = account.id;
Expand All @@ -45,7 +45,7 @@ authApi.get('/redirection', async (c) => {
}

return c.json({
defaultOrgShortCode:
defaultOrgShortcode:
accountResponse?.orgMemberships[0]?.org?.shortcode || null
});
});
Expand Down
4 changes: 2 additions & 2 deletions apps/platform/routes/realtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Hono } from 'hono';
import type { Ctx } from '~platform/ctx';
import { zValidator } from '@hono/zod-validator';
import { z } from 'zod';
import { validateOrgShortCode } from '~platform/utils/orgShortCode';
import { validateOrgShortcode } from '~platform/utils/orgShortcode';
import { db } from '@u22n/database';
import { and, eq } from '@u22n/database/orm';
import { orgMembers } from '@u22n/database/schema';
Expand All @@ -16,7 +16,7 @@ realtimeApi.post(
zValidator('header', z.object({ 'org-shortcode': z.string() })),
async (c) => {
const accountContext = c.get('account');
const orgContext = await validateOrgShortCode(
const orgContext = await validateOrgShortcode(
c.req.valid('header')['org-shortcode']
);

Expand Down
6 changes: 3 additions & 3 deletions apps/platform/trpc/routers/authRouter/passwordRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export const passwordRouter = router({
.output(
z.object({
status: z.enum(['NO_2FA_SETUP', '2FA_REQUIRED']),
defaultOrgShortCode: z.string().optional()
defaultOrgShortcode: z.string().optional()
})
)
.mutation(async ({ ctx, input }) => {
Expand Down Expand Up @@ -207,7 +207,7 @@ export const passwordRouter = router({
);
return {
status: '2FA_REQUIRED',
defaultOrgShortCode: userResponse.orgMemberships[0]?.org.shortcode
defaultOrgShortcode: userResponse.orgMemberships[0]?.org.shortcode
};
} else {
await createLuciaSessionCookie(ctx.event, {
Expand All @@ -217,7 +217,7 @@ export const passwordRouter = router({
});
return {
status: 'NO_2FA_SETUP',
defaultOrgShortCode: userResponse.orgMemberships[0]?.org.shortcode
defaultOrgShortcode: userResponse.orgMemberships[0]?.org.shortcode
};
}
}),
Expand Down
2 changes: 1 addition & 1 deletion apps/platform/trpc/routers/orgRouter/mail/domainsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const domainsRouter = router({
const iCanHazCaller = iCanHazCallerFactory(ctx);

const canHazDomain = await iCanHazCaller.domain({
orgShortCode: input.orgShortCode
orgShortcode: input.orgShortcode
});
if (!canHazDomain) {
throw new TRPCError({
Expand Down
18 changes: 9 additions & 9 deletions apps/platform/trpc/routers/orgRouter/orgCrudRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { typeIdGenerator } from '@u22n/utils/typeid';
import { TRPCError } from '@trpc/server';
import { blockedUsernames, reservedUsernames } from '~platform/utils/signup';

async function validateOrgShortCode(
async function validateOrgShortcode(
db: DBType,
shortcode: string
): Promise<{
Expand Down Expand Up @@ -49,7 +49,7 @@ async function validateOrgShortCode(
}

export const crudRouter = router({
checkShortCodeAvailability: accountProcedure
checkShortcodeAvailability: accountProcedure
.input(
z.object({
shortcode: z
Expand All @@ -62,14 +62,14 @@ export const crudRouter = router({
})
)
.query(async ({ ctx, input }) => {
return await validateOrgShortCode(ctx.db, input.shortcode);
return await validateOrgShortcode(ctx.db, input.shortcode);
}),

createNewOrg: accountProcedure
.input(
z.object({
orgName: z.string().min(3).max(32),
orgShortCode: z
orgShortcode: z
.string()
.min(5)
.max(64)
Expand All @@ -82,9 +82,9 @@ export const crudRouter = router({
const { db, account } = ctx;
const accountId = account.id;

const shortcodeAvailability = await validateOrgShortCode(
const shortcodeAvailability = await validateOrgShortcode(
db,
input.orgShortCode
input.orgShortcode
);

if (!shortcodeAvailability.available) {
Expand All @@ -99,7 +99,7 @@ export const crudRouter = router({
const insertOrgResponse = await db.insert(orgs).values({
ownerId: accountId,
name: input.orgName,
shortcode: input.orgShortCode,
shortcode: input.orgShortcode,
publicId: newPublicId
});
const orgId = +insertOrgResponse.insertId;
Expand Down Expand Up @@ -184,13 +184,13 @@ export const crudRouter = router({
}
});

const adminOrgShortCodes = orgMembersQuery
const adminOrgShortcodes = orgMembersQuery
.filter((orgMember) => orgMember.role === 'admin')
.map((orgMember) => orgMember.org.shortcode);

return {
userOrgs: orgMembersQuery,
adminOrgShortCodes: adminOrgShortCodes
adminOrgShortcodes: adminOrgShortcodes
};
})
});
4 changes: 2 additions & 2 deletions apps/platform/trpc/routers/orgRouter/orgStoreRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ export const storeRouter = router({
({ org, profile }) => ({
name: org.name,
publicId: org.publicId,
shortCode: org.shortcode,
shortcode: org.shortcode,
avatarTimestamp: org.avatarTimestamp,
orgMemberProfile: profile
})
);

const currentOrg = orgsTransformed.find(
(o) => o.shortCode === input.orgShortCode
(o) => o.shortcode === input.orgShortcode
);

if (!currentOrg) {
Expand Down
8 changes: 4 additions & 4 deletions apps/platform/trpc/routers/orgRouter/users/invitesRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from '@u22n/database/schema';
import { typeIdGenerator, typeIdValidator } from '@u22n/utils/typeid';
import { nanoIdToken, zodSchemas } from '@u22n/utils/zodSchemas';
import { refreshOrgShortCodeCache } from '~platform/utils/orgShortCode';
import { refreshOrgShortcodeCache } from '~platform/utils/orgShortcode';
import { isAccountAdminOfOrg } from '~platform/utils/account';
import { TRPCError } from '@trpc/server';
import { billingTrpcClient } from '~platform/utils/tRPCServerClients';
Expand Down Expand Up @@ -343,7 +343,7 @@ export const invitesRouter = router({
orgPublicId: queryInvitesResponse.org.publicId,
orgAvatarTimestamp: queryInvitesResponse.org.avatarTimestamp,
orgName: queryInvitesResponse.org.name,
orgShortCode: queryInvitesResponse.org.shortcode,
orgShortcode: queryInvitesResponse.org.shortcode,
loggedIn: userLoggedIn,
username: username
};
Expand Down Expand Up @@ -466,11 +466,11 @@ export const invitesRouter = router({
});
}

await refreshOrgShortCodeCache(+queryInvitesResponse.orgId);
await refreshOrgShortcodeCache(+queryInvitesResponse.orgId);

return {
success: true,
orgShortCode: queryInvitesResponse.org.shortcode
orgShortcode: queryInvitesResponse.org.shortcode
};
}),
invalidateInvite: orgAdminProcedure
Expand Down
2 changes: 1 addition & 1 deletion apps/platform/trpc/routers/userRouter/defaultsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const defaultsRouter = router({
const userHasOrgMembership = accountResponse.orgMemberships.length > 0;

return {
defaultOrgShortCode:
defaultOrgShortcode:
accountResponse?.orgMemberships[0]?.org?.shortcode || null,
twoFactorEnabledCorrectly: twoFactorEnabledCorrectly,
userHasOrgMembership: userHasOrgMembership,
Expand Down
10 changes: 5 additions & 5 deletions apps/platform/trpc/routers/userRouter/profileRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,28 @@ export const profileRouter = router({
.input(
z.object({
orgPublicId: typeIdValidator('org').optional(),
orgShortCode: z.string().min(1).max(32).optional()
orgShortcode: z.string().min(1).max(32).optional()
})
)
.query(async ({ ctx, input }) => {
const { db, account } = ctx;
const accountId = account.id;

let orgId: number | null = null;
if (input.orgPublicId || input.orgShortCode) {
if (input.orgPublicId || input.orgShortcode) {
const orgQuery = await db.query.orgs.findFirst({
where: input.orgPublicId
? eq(orgs.publicId, input.orgPublicId)
: input.orgShortCode
? eq(orgs.shortcode, input.orgShortCode)
: input.orgShortcode
? eq(orgs.shortcode, input.orgShortcode)
: eq(orgs.id, 0),
columns: {
id: true
}
});
orgId = orgQuery?.id || null;
}
if ((input.orgPublicId || input.orgShortCode) && !orgId) {
if ((input.orgPublicId || input.orgShortcode) && !orgId) {
throw new TRPCError({
code: 'NOT_FOUND',
message:
Expand Down
12 changes: 6 additions & 6 deletions apps/platform/trpc/trpc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TRPCError, initTRPC } from '@trpc/server';
import superjson from 'superjson';
import { validateOrgShortCode } from '~platform/utils/orgShortCode';
import { validateOrgShortcode } from '~platform/utils/orgShortcode';
import type { TrpcContext } from '~platform/ctx';
import { z } from 'zod';
import { env } from '~platform/env';
Expand Down Expand Up @@ -53,14 +53,14 @@ export const accountProcedure = publicProcedure.use(isAccountAuthenticated);

export const orgProcedure = publicProcedure
.use(isAccountAuthenticated)
.input(z.object({ orgShortCode: z.string() }))
.input(z.object({ orgShortcode: z.string() }))
.use(({ input, ctx, next }) =>
ctx.event
.get('otel')
.tracer.startActiveSpan(`Validate orgShortCode`, async (span) => {
const { orgShortCode } = input;
span.setAttribute('org.shortCode', orgShortCode);
const orgData = await validateOrgShortCode(orgShortCode);
.tracer.startActiveSpan(`Validate orgShortcode`, async (span) => {
const { orgShortcode } = input;
span.setAttribute('org.shortcode', orgShortcode);
const orgData = await validateOrgShortcode(orgShortcode);

if (!orgData) {
span.setAttributes({ 'org.found': false });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import { orgs } from '@u22n/database/schema';
import type { OrgContext } from '~platform/ctx';
import { storage } from '~platform/storage';

export async function validateOrgShortCode(orgShortCode: string) {
if (!orgShortCode) return null;
export async function validateOrgShortcode(orgShortcode: string) {
if (!orgShortcode) return null;

const cachedShortCodeOrgContext =
await storage.orgContext.getItem(orgShortCode);
if (cachedShortCodeOrgContext) {
return cachedShortCodeOrgContext;
const cachedShortcodeOrgContext =
await storage.orgContext.getItem(orgShortcode);
if (cachedShortcodeOrgContext) {
return cachedShortcodeOrgContext;
}

const orgLookupResult = await db.query.orgs.findFirst({
where: eq(orgs.shortcode, orgShortCode),
where: eq(orgs.shortcode, orgShortcode),
columns: { id: true, publicId: true, name: true },
with: {
members: {
Expand All @@ -38,11 +38,11 @@ export async function validateOrgShortCode(orgShortCode: string) {
name: orgLookupResult.name
};

await storage.orgContext.setItem(orgShortCode, orgContext);
await storage.orgContext.setItem(orgShortcode, orgContext);
return orgContext;
}

export async function refreshOrgShortCodeCache(orgId: number) {
export async function refreshOrgShortcodeCache(orgId: number) {
const orgLookupResult = await db.query.orgs.findFirst({
where: eq(orgs.id, orgId),
columns: { id: true, publicId: true, shortcode: true, name: true },
Expand Down
4 changes: 2 additions & 2 deletions apps/storage/api/presign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ export const presignApi = new Hono<Ctx>().get(
checkSignedIn,
zValidator(
'query',
z.object({ filename: z.string(), orgShortCode: z.string() })
z.object({ filename: z.string(), orgShortcode: z.string() })
),
async (c) => {
const accountId = c.get('account')?.id!; // we know it's not null here, checked in the middleware
const data = c.req.valid('query');

const orgQueryResponse = await db.query.orgs.findFirst({
where: eq(orgs.shortcode, data.orgShortCode),
where: eq(orgs.shortcode, data.orgShortcode),
columns: {
id: true,
publicId: true
Expand Down
8 changes: 4 additions & 4 deletions apps/storage/proxy/attachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import { convoAttachments, orgMembers, orgs } from '@u22n/database/schema';
import type { Ctx } from '../ctx';

export const attachmentProxy = new Hono<Ctx>().get(
'/:orgShortCode/:attachmentId/:filename',
'/:orgShortcode/:attachmentId/:filename',
zValidator(
'param',
z.object({
filename: z.string().transform((f) => decodeURIComponent(f)),
attachmentId: typeIdValidator('convoAttachments'),
orgShortCode: z.string()
orgShortcode: z.string()
})
),
async (c) => {
const { filename, attachmentId, orgShortCode } = c.req.valid('param');
const { filename, attachmentId, orgShortcode } = c.req.valid('param');

const attachmentQueryResponse = await db.query.convoAttachments.findFirst({
where: eq(convoAttachments.publicId, attachmentId),
Expand All @@ -44,7 +44,7 @@ export const attachmentProxy = new Hono<Ctx>().get(
}

const orgQueryResponse = await db.query.orgs.findFirst({
where: eq(orgs.shortcode, orgShortCode),
where: eq(orgs.shortcode, orgShortcode),
columns: {
id: true,
publicId: true
Expand Down
Loading

0 comments on commit 7d10378

Please sign in to comment.