Skip to content

Commit

Permalink
Merge pull request #2125 from ever-co/feat/static-build-support-9
Browse files Browse the repository at this point in the history
Feat/Next-Frontend APIs
  • Loading branch information
evereq authored Jan 26, 2024
2 parents 36c7dc6 + 00e3017 commit 2e48d41
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 46 deletions.
21 changes: 21 additions & 0 deletions apps/web/app/api/organization-projects/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { authenticatedGuard } from '@app/services/server/guards/authenticated-guard-app';

import { createOrganizationProjectRequest } from '@app/services/server/requests';
import { NextResponse } from 'next/server';

export async function POST(req: Request) {
const res = new NextResponse();
const { $res, user, access_token } = await authenticatedGuard(req, res);

if (!user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });

const body: {
name: string;
tenantId: string;
organizationId: string;
} = await req.json();

const response = await createOrganizationProjectRequest(body, access_token);

return $res(response.data);
}
6 changes: 6 additions & 0 deletions apps/web/app/api/organization-team/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export async function POST(req: Request) {
if ($name.trim().length < 2) {
return NextResponse.json({ errors: { name: 'Invalid team name !' } }, { status: 400 });
}

await createOrganizationTeamRequest(
{
name: $name,
Expand All @@ -32,6 +33,11 @@ export async function POST(req: Request) {
);
}

if (<boolean>true) {
const teams = await getAllOrganizationTeamRequest({ tenantId, organizationId: organizationId }, access_token);
return $res(teams.data);
}

const { data: organizations } = await getUserOrganizationsRequest({ tenantId, userId: user.id }, access_token);

const organizationsItems = organizations.items;
Expand Down
32 changes: 16 additions & 16 deletions apps/web/app/api/tasks/employee/[employeeId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ export async function GET(req: Request, { params }: { params: { employeeId: stri
organizationTeamId: string;
};

return $res(
await getEmployeeTasksRequest({
tenantId,
employeeId,
organizationTeamId,
bearer_token
})
);
const response = await getEmployeeTasksRequest({
tenantId,
employeeId,
organizationTeamId,
bearer_token
});

return $res(response.data);
}

export async function DELETE(req: Request, { params }: { params: { employeeId: string } }) {
Expand All @@ -36,12 +36,12 @@ export async function DELETE(req: Request, { params }: { params: { employeeId: s
organizationTeamId: string;
};

return $res(
await deleteEmployeeFromTasksRequest({
tenantId,
employeeId,
organizationTeamId,
bearer_token
})
);
const response = await deleteEmployeeFromTasksRequest({
tenantId,
employeeId,
organizationTeamId,
bearer_token
});

return $res(response.data);
}
2 changes: 2 additions & 0 deletions apps/web/app/api/tasks/team/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ export async function POST(req: Request) {
const body: Record<string, any> = (await req.json()) || {};

const title = body.title?.trim() || '';

if (title.trim().length < 2) {
return $res({ errors: { name: 'Invalid task name !' } });
}

const activeTeam = getActiveTeamIdCookie({ req, res });

await createTaskRequest({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function GET(req: Request) {
const { $res, user, tenantId, organizationId, access_token } = await authenticatedGuard(req, res);
if (!user) return $res('Unauthorized');

const { data } = await tasksTimesheetStatisticsRequest(
const response = await tasksTimesheetStatisticsRequest(
{
tenantId,
organizationId,
Expand All @@ -17,5 +17,5 @@ export async function GET(req: Request) {
access_token
);

return $res(data);
return $res(response.data);
}
1 change: 1 addition & 0 deletions apps/web/app/hooks/features/useAuthenticateUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const useAuthenticateUser = (defaultUser?: IUser) => {
}, []);

return {
$user,
user: $user.current,
setUser,
isTeamManager,
Expand Down
6 changes: 3 additions & 3 deletions apps/web/app/hooks/features/useOrganizationTeams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function useCreateOrganizationTeam() {
const [teams, setTeams] = useRecoilState(organizationTeamsState);
const teamsRef = useSyncRef(teams);
const setActiveTeamId = useSetRecoilState(activeTeamIdState);
const { refreshToken } = useAuthenticateUser();
const { refreshToken, $user } = useAuthenticateUser();
const [isTeamMember, setIsTeamMember] = useRecoilState(isTeamMemberState);

const createOrganizationTeam = useCallback(
Expand All @@ -83,11 +83,11 @@ function useCreateOrganizationTeam() {
const $name = name.trim();
const exits = teams.find((t) => t.name.toLowerCase() === $name.toLowerCase());

if (exits || $name.length < 2) {
if (exits || $name.length < 2 || !$user.current) {
return Promise.reject(new Error('Invalid team name !'));
}

return queryCall($name).then(async (res) => {
return queryCall($name, $user.current).then(async (res) => {
const dt = res.data?.items || [];
setTeams(dt);
const created = dt.find((t) => t.name === $name);
Expand Down
39 changes: 21 additions & 18 deletions apps/web/app/hooks/features/useTeamTasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { useAuthenticateUser } from './useAuthenticateUser';

export function useTeamTasks() {
const { updateOrganizationTeamEmployeeActiveTask } = useOrganizationEmployeeTeams();
const { user } = useAuthenticateUser();
const { user, $user } = useAuthenticateUser();

const setAllTasks = useSetRecoilState(teamTasksState);
const tasks = useRecoilValue(tasksByTeamState);
Expand Down Expand Up @@ -230,23 +230,26 @@ export function useTeamTasks() {
},
members?: { id: string }[]
) => {
return createQueryCall({
title: taskName,
issueType,
status,
priority,
size,
tags,
// Set Project Id to cookie
// TODO: Make it dynamic when we add Dropdown in Navbar
...(activeTeam?.projects && activeTeam?.projects.length > 0
? {
projectId: activeTeam.projects[0].id
}
: {}),
...(description ? { description: `<p>${description}</p>` } : {}),
...(members ? { members } : {})
}).then((res) => {
return createQueryCall(
{
title: taskName,
issueType,
status,
priority,
size,
tags,
// Set Project Id to cookie
// TODO: Make it dynamic when we add Dropdown in Navbar
...(activeTeam?.projects && activeTeam?.projects.length > 0
? {
projectId: activeTeam.projects[0].id
}
: {}),
...(description ? { description: `<p>${description}</p>` } : {}),
...(members ? { members } : {})
},
$user.current
).then((res) => {
deepCheckAndUpdateTasks(res?.data?.items || [], true);
return res;
});
Expand Down
1 change: 1 addition & 0 deletions apps/web/app/services/client/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ export * from './organization-projects';
export * from './activity/time-slots';
export * from './activity/activity';
export * from './default';
export * from './projects';
38 changes: 35 additions & 3 deletions apps/web/app/services/client/api/organization-team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import {
IOrganizationTeamUpdate,
IOrganizationTeam,
TimerSource,
OT_Member
OT_Member,
IOrganizationTeamCreate,
IUser
} from '@app/interfaces';
import moment from 'moment';
import api, { deleteApi, get, put } from '../axios';
import api, { deleteApi, get, post, put } from '../axios';
import { GAUZY_API_BASE_SERVER_URL } from '@app/constants';
import { getOrganizationIdCookie, getTenantIdCookie } from '@app/helpers';
import { createOrganizationProjectAPI } from './projects';

export async function getOrganizationTeamsAPI(organizationId: string, tenantId: string) {
const relations = [
Expand Down Expand Up @@ -40,7 +43,36 @@ export async function getOrganizationTeamsAPI(organizationId: string, tenantId:
return get<PaginationResponse<IOrganizationTeamList>>(endpoint, { tenantId });
}

export function createOrganizationTeamAPI(name: string) {
export async function createOrganizationTeamAPI(name: string, user: IUser) {
const $name = name.trim();

if (GAUZY_API_BASE_SERVER_URL.value) {
const tenantId = getTenantIdCookie();
const organizationId = getOrganizationIdCookie();

const datas: IOrganizationTeamCreate = {
name: $name,
tenantId,
organizationId,
managerIds: user?.employee?.id ? [user.employee.id] : [],
public: true
};

const project = await createOrganizationProjectAPI({
name: $name,
tenantId,
organizationId
});

datas.projects = [project.data];

await post('/organization-team', datas, {
tenantId
});

return getOrganizationTeamsAPI(organizationId, tenantId);
}

return api.post<PaginationResponse<IOrganizationTeamList>>('/organization-team', { name });
}

Expand Down
12 changes: 12 additions & 0 deletions apps/web/app/services/client/api/projects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { IProject } from '@app/interfaces';
import { post } from '../axios';

type Params = {
name: string;
tenantId: string;
organizationId: string;
};

export function createOrganizationProjectAPI(params: Params) {
return post<IProject>(`/organization-projects`, params);
}
68 changes: 64 additions & 4 deletions apps/web/app/services/client/api/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
import { DeleteResponse, PaginationResponse } from '@app/interfaces/IDataResponse';
import { ICreateTask, ITeamTask } from '@app/interfaces/ITask';
import { ITasksTimesheet } from '@app/interfaces/ITimer';
import api, { deleteApi, get, put } from '../axios';
import api, { deleteApi, get, post, put } from '../axios';
import { GAUZY_API_BASE_SERVER_URL } from '@app/constants';
import {
getActiveProjectIdCookie,
getActiveTeamIdCookie,
getOrganizationIdCookie,
getTenantIdCookie
} from '@app/helpers';
import { IUser } from '@app/interfaces';
import { TTasksTimesheetStatisticsParams } from '@app/services/server/requests';

export function getTasksByIdAPI(taskId: string) {
const organizationId = getOrganizationIdCookie();
Expand Down Expand Up @@ -105,7 +107,38 @@ export async function updateTaskAPI(taskId: string, body: Partial<ITeamTask>) {
return put<PaginationResponse<ITeamTask>>(`/tasks/${taskId}`, body);
}

export function createTeamTaskAPI(body: Partial<ICreateTask> & { title: string }) {
export async function createTeamTaskAPI(body: Partial<ICreateTask> & { title: string }, user: IUser | undefined) {
if (GAUZY_API_BASE_SERVER_URL.value) {
const organizationId = getOrganizationIdCookie();
const teamId = getActiveTeamIdCookie();
const tenantId = getTenantIdCookie();
const projectId = getActiveProjectIdCookie();

const title = body.title.trim() || '';

const datas: ICreateTask = {
description: '',
status: 'open',
members: user?.employee?.id ? [{ id: user.employee.id }] : [],
teams: [
{
id: teamId
}
],
tags: [],
organizationId,
tenantId,
projectId,
estimate: 0,
...body,
title // this must be called after ...body
};

await post('/tasks', datas, { tenantId });

return getTeamTasksAPI(organizationId, tenantId, projectId, teamId);
}

return api.post<PaginationResponse<ITeamTask>>('/tasks/team', body);
}

Expand Down Expand Up @@ -205,13 +238,40 @@ export async function activeTaskTimesheetStatisticsAPI(
}

export function allTaskTimesheetStatisticsAPI() {
if (GAUZY_API_BASE_SERVER_URL.value) {
const tenantId = getTenantIdCookie();
const organizationId = getOrganizationIdCookie();

const params: TTasksTimesheetStatisticsParams = {
tenantId,
organizationId,
employeeIds: [],
defaultRange: 'false'
};

const { employeeIds, ...rest } = params;

const queries = new URLSearchParams({
...rest,
...employeeIds.reduce(
(acc, v, i) => {
acc[`employeeIds[${i}]`] = v;
return acc;
},
{} as Record<string, any>
)
});

return get<ITasksTimesheet[]>(`/timesheet/statistics/tasks?${queries.toString()}`, { tenantId });
}

return api.get<ITasksTimesheet[]>(`/timer/timesheet/all-statistics-tasks`);
}

export function deleteEmployeeFromTasksAPI(employeeId: string, organizationTeamId: string) {
return api.delete<DeleteResponse>(`/tasks/employee/${employeeId}?organizationTeamId=${organizationTeamId}`);
return deleteApi<DeleteResponse>(`/tasks/employee/${employeeId}?organizationTeamId=${organizationTeamId}`);
}

export function getTasksByEmployeeIdAPI(employeeId: string, organizationTeamId: string) {
return api.get<ITeamTask[]>(`/tasks/employee/${employeeId}?organizationTeamId=${organizationTeamId}`);
return get<ITeamTask[]>(`/tasks/employee/${employeeId}?organizationTeamId=${organizationTeamId}`);
}
1 change: 1 addition & 0 deletions apps/web/app/services/server/requests/organization-team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export async function createOrganizationTeamRequest(datas: IOrganizationTeamCrea
},
bearer_token
);

datas.projects = [project];

return serverFetch<IOrganizationTeam>({
Expand Down

0 comments on commit 2e48d41

Please sign in to comment.