Repository navigation
feat(org): implement outbound organization creation with API delegation #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,13 +1,59 @@ | ||||||||||||||||||||||||||||||||||||||||||
| 'use client'; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| import React, { FC, useCallback, useMemo } from 'react'; | ||||||||||||||||||||||||||||||||||||||||||
| import React, { FC, useCallback, useMemo, useState } from 'react'; | ||||||||||||||||||||||||||||||||||||||||||
| import { useFetch } from '@gitroom/helpers/utils/custom.fetch'; | ||||||||||||||||||||||||||||||||||||||||||
| import useSWR from 'swr'; | ||||||||||||||||||||||||||||||||||||||||||
| import { useUser } from '@gitroom/frontend/components/layout/user.context'; | ||||||||||||||||||||||||||||||||||||||||||
| import clsx from 'clsx'; | ||||||||||||||||||||||||||||||||||||||||||
| import { useModals } from '@gitroom/frontend/components/layout/new-modal'; | ||||||||||||||||||||||||||||||||||||||||||
| import { Input } from '@gitroom/react/form/input'; | ||||||||||||||||||||||||||||||||||||||||||
| import { Button } from '@gitroom/react/form/button'; | ||||||||||||||||||||||||||||||||||||||||||
| import { useT } from '@gitroom/react/translation/get.transation.service.client'; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| export const CreateOrganization = () => { | ||||||||||||||||||||||||||||||||||||||||||
| const t = useT(); | ||||||||||||||||||||||||||||||||||||||||||
| const fetch = useFetch(); | ||||||||||||||||||||||||||||||||||||||||||
| const modals = useModals(); | ||||||||||||||||||||||||||||||||||||||||||
| const [name, setName] = useState(''); | ||||||||||||||||||||||||||||||||||||||||||
| const [loading, setLoading] = useState(false); | ||||||||||||||||||||||||||||||||||||||||||
| const create = useCallback(async () => { | ||||||||||||||||||||||||||||||||||||||||||
| setLoading(true); | ||||||||||||||||||||||||||||||||||||||||||
| const { id } = await ( | ||||||||||||||||||||||||||||||||||||||||||
| await fetch('/user/organizations', { | ||||||||||||||||||||||||||||||||||||||||||
| method: 'POST', | ||||||||||||||||||||||||||||||||||||||||||
| body: JSON.stringify({ name }), | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| ).json(); | ||||||||||||||||||||||||||||||||||||||||||
| await fetch('/user/change-org', { | ||||||||||||||||||||||||||||||||||||||||||
| method: 'POST', | ||||||||||||||||||||||||||||||||||||||||||
| body: JSON.stringify({ id }), | ||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
| modals.closeAll(); | ||||||||||||||||||||||||||||||||||||||||||
| window.location.reload(); | ||||||||||||||||||||||||||||||||||||||||||
| }, [name]); | ||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||
| <div className="relative flex gap-[10px] flex-col flex-1 p-[16px] pt-0"> | ||||||||||||||||||||||||||||||||||||||||||
| <Input | ||||||||||||||||||||||||||||||||||||||||||
| value={name} | ||||||||||||||||||||||||||||||||||||||||||
| disableForm={true} | ||||||||||||||||||||||||||||||||||||||||||
| removeError={true} | ||||||||||||||||||||||||||||||||||||||||||
| onChange={(e) => setName(e.target.value)} | ||||||||||||||||||||||||||||||||||||||||||
| name="name" | ||||||||||||||||||||||||||||||||||||||||||
| label={t('organization_name', 'Organization name')} | ||||||||||||||||||||||||||||||||||||||||||
| placeholder={t('organization_name', 'Organization name')} | ||||||||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||||||||
| <Button type="button" className="mt-[18px]" onClick={create} disabled={loading}> | ||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prevent users from submitting empty or whitespace-only organization names by adding validation to the button's
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
| {t('create', 'Create')} | ||||||||||||||||||||||||||||||||||||||||||
| </Button> | ||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| export const OrganizationSelector: FC<{ asOpenSelect?: boolean }> = ({ | ||||||||||||||||||||||||||||||||||||||||||
| asOpenSelect, | ||||||||||||||||||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||||||||||||||||||
| const t = useT(); | ||||||||||||||||||||||||||||||||||||||||||
| const modals = useModals(); | ||||||||||||||||||||||||||||||||||||||||||
| const fetch = useFetch(); | ||||||||||||||||||||||||||||||||||||||||||
| const user = useUser(); | ||||||||||||||||||||||||||||||||||||||||||
| const load = useCallback(async () => { | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -38,7 +84,17 @@ export const OrganizationSelector: FC<{ asOpenSelect?: boolean }> = ({ | |||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||
| [] | ||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||
| if (isLoading || (!isLoading && data?.length === 1)) { | ||||||||||||||||||||||||||||||||||||||||||
| const createOrg = useCallback(() => { | ||||||||||||||||||||||||||||||||||||||||||
| modals.openModal({ | ||||||||||||||||||||||||||||||||||||||||||
| classNames: { | ||||||||||||||||||||||||||||||||||||||||||
| modal: 'bg-transparent text-textColor', | ||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||
| title: t('create_new_organization', 'Create New Organization'), | ||||||||||||||||||||||||||||||||||||||||||
| withCloseButton: true, | ||||||||||||||||||||||||||||||||||||||||||
| children: <CreateOrganization />, | ||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
| }, [t]); | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+87
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
| if (isLoading) { | ||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -68,7 +124,7 @@ export const OrganizationSelector: FC<{ asOpenSelect?: boolean }> = ({ | |||||||||||||||||||||||||||||||||||||||||
| )} | ||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||
| )} | ||||||||||||||||||||||||||||||||||||||||||
| {data?.length > 1 && ( | ||||||||||||||||||||||||||||||||||||||||||
| {(data?.length > 1 || !asOpenSelect) && ( | ||||||||||||||||||||||||||||||||||||||||||
| <div | ||||||||||||||||||||||||||||||||||||||||||
| className={clsx( | ||||||||||||||||||||||||||||||||||||||||||
| 'hidden py-[12px] px-[12px] group-hover:flex absolute top-[100%] end-0 w-max max-w-[400px] bg-third border-tableBorder border gap-[12px] cursor-pointer flex-col', | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -102,6 +158,11 @@ export const OrganizationSelector: FC<{ asOpenSelect?: boolean }> = ({ | |||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
| )} | ||||||||||||||||||||||||||||||||||||||||||
| {!asOpenSelect && ( | ||||||||||||||||||||||||||||||||||||||||||
| <div onClick={createOrg} className="whitespace-nowrap font-medium text-primary hover:underline"> | ||||||||||||||||||||||||||||||||||||||||||
| {t('create_new_organization', 'Create New Organization')} + | ||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||
| )} | ||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||
| )} | ||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -218,6 +218,47 @@ export class OrganizationService { | |
| return this._organizationRepository.deleteOrganization(orgId); | ||
| } | ||
|
|
||
| async createOrgForUser( | ||
| userId: string, | ||
| body: { name?: string }, | ||
| userAuthHeader?: string | ||
| ) { | ||
| let orgId: string | undefined; | ||
| let orgName = body.name || 'New Organization'; | ||
|
|
||
| // If user is authenticated via DOS ID, delegate creation to api.dos.me (Method 3: Delegated Creation) | ||
| const apiUrl = process.env.POSTIZ_OAUTH_URL || 'https://api.dos.me'; | ||
| if (userAuthHeader && userAuthHeader.startsWith('Bearer ')) { | ||
| try { | ||
| const response = await fetch(`${apiUrl}/organizations`, { | ||
| method: 'POST', | ||
| headers: { | ||
| Authorization: userAuthHeader, | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify({ | ||
| name: orgName, | ||
| }), | ||
| }); | ||
| if (response.ok) { | ||
| const data = await response.json(); | ||
| if (data?.id) { | ||
| orgId = data.id; | ||
| orgName = data.name || orgName; | ||
| } | ||
| } | ||
| } catch (err) { | ||
| // Fallback to local creation if remote call fails | ||
| } | ||
| } | ||
|
Comment on lines
+231
to
+253
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The external if (userAuthHeader && userAuthHeader.startsWith('Bearer ')) {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000);
try {
const response = await fetch(apiUrl + '/organizations', {
method: 'POST',
headers: {
Authorization: userAuthHeader,
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: orgName,
}),
signal: controller.signal,
});
clearTimeout(timeoutId);
if (response.ok) {
const data = await response.json();
if (data?.id) {
orgId = data.id;
orgName = data.name || orgName;
}
}
} catch (err) {
clearTimeout(timeoutId);
// Fallback to local creation if remote call fails
}
} |
||
|
|
||
| return this._organizationRepository.createOrgForUser(userId, orgName, orgId); | ||
| } | ||
|
|
||
| async getOrganizationName(orgId: string) { | ||
| return this._organizationRepository.getOrganizationName(orgId); | ||
| } | ||
|
|
||
| async findOrgByName(name: string) { | ||
| return this._organizationRepository.findOrgByName(name); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { IsOptional, IsString, MaxLength } from 'class-validator'; | ||
|
|
||
| export class CreateOrganizationDto { | ||
| @IsString() | ||
| @IsOptional() | ||
| @MaxLength(100) | ||
| name?: string; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
createcallback lacks error handling and does not reset theloadingstate if the request fails, which would leave the UI permanently disabled. Additionally,fetchandmodalsare missing from the dependency array ofuseCallback.