|
1 | 1 | import type { Request, Response } from 'express' |
2 | 2 | import { z } from 'zod' |
3 | 3 |
|
4 | | -import { NotFoundError, normalizeHostname } from '@crowd/common' |
5 | | -import { |
6 | | - OrgIdentityField, |
7 | | - OrganizationField, |
8 | | - findOrgAttributes, |
9 | | - findOrgById, |
10 | | - optionsQx, |
11 | | - queryOrgIdentities, |
12 | | -} from '@crowd/data-access-layer' |
13 | | -import { OrganizationIdentityType } from '@crowd/types' |
| 4 | +import { BadRequestError, NotFoundError, normalizeHostname } from '@crowd/common' |
| 5 | +import { findOrganizationByNameOrDomain, optionsQx } from '@crowd/data-access-layer' |
14 | 6 |
|
15 | 7 | import { ok } from '@/utils/api' |
16 | 8 | import { validateOrThrow } from '@/utils/validation' |
17 | 9 |
|
18 | | -const querySchema = z.object({ |
19 | | - domain: z.string().trim().min(1), |
20 | | -}) |
| 10 | +const querySchema = z |
| 11 | + .object({ |
| 12 | + name: z.string().trim().min(1).optional(), |
| 13 | + domain: z.string().trim().min(1).optional(), |
| 14 | + }) |
| 15 | + .refine((data) => data.name || data.domain, { |
| 16 | + message: 'Either name or domain must be provided', |
| 17 | + }) |
21 | 18 |
|
22 | 19 | export async function getOrganization(req: Request, res: Response): Promise<void> { |
23 | | - const { domain } = validateOrThrow(querySchema, req.query) |
| 20 | + const { name, domain: rawDomain } = validateOrThrow(querySchema, req.query) |
| 21 | + |
| 22 | + const domain = rawDomain ? normalizeHostname(rawDomain, false) : undefined |
| 23 | + |
| 24 | + if (rawDomain && !domain) { |
| 25 | + throw new BadRequestError(`Invalid domain: ${rawDomain}`) |
| 26 | + } |
24 | 27 |
|
25 | 28 | const qx = optionsQx(req) |
26 | 29 |
|
27 | | - const results = await queryOrgIdentities(qx, { |
28 | | - fields: [OrgIdentityField.ORGANIZATION_ID], |
29 | | - filter: { |
30 | | - and: [ |
31 | | - { value: { eq: normalizeHostname(domain, false) } }, |
32 | | - { type: { eq: OrganizationIdentityType.PRIMARY_DOMAIN } }, |
33 | | - { verified: { eq: true } }, |
34 | | - ], |
35 | | - }, |
| 30 | + const organization = await findOrganizationByNameOrDomain(qx, { |
| 31 | + name, |
| 32 | + domain, |
36 | 33 | }) |
37 | 34 |
|
38 | | - const organizationId = results[0]?.organizationId |
39 | | - |
40 | | - if (!organizationId) { |
| 35 | + if (!organization) { |
41 | 36 | throw new NotFoundError('Organization not found') |
42 | 37 | } |
43 | 38 |
|
44 | | - const org = await findOrgById(qx, organizationId, [ |
45 | | - OrganizationField.ID, |
46 | | - OrganizationField.DISPLAY_NAME, |
47 | | - ]) |
48 | | - |
49 | | - const attributes = await findOrgAttributes(qx, organizationId) |
50 | | - const logo = attributes.find((a) => a.name === 'logo')?.value |
51 | | - |
52 | | - ok(res, { |
53 | | - id: org.id, |
54 | | - name: org.displayName, |
55 | | - ...(logo ? { logo } : {}), |
56 | | - }) |
| 39 | + const { logo, ...rest } = organization |
| 40 | + ok(res, { ...rest, ...(logo ? { logo } : {}) }) |
57 | 41 | } |
0 commit comments