Skip to content

Commit

Permalink
update api endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoalzate committed May 10, 2024
1 parent ccad72a commit ab16af3
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 14 deletions.
File renamed without changes.
4 changes: 2 additions & 2 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export { default as fetchRegistrationData } from './fetchRegistrationData';
export { default as fetchRegistrationFields } from './fetchRegistrationFields';
export { default as fetchRegistrations } from './fetchRegistrations';
export { default as fetchUserAttributes } from './fetchUserAttributes';
export { default as fetchUserData } from './fetchUserData';
export { default as fetchUser } from './fetchUser';
export { default as fetchUserGroups } from './fetchUserGroups';
export { default as fetchUserRegistrations } from './fetchUserRegistrations';
export { default as fetchUserVotes } from './fetchUserVotes';
Expand All @@ -29,5 +29,5 @@ export { default as postRegistration } from './postRegistration';
export { default as postUserToGroups } from './postUserToGroups';
export { default as postVotes } from './postVotes';
export { default as putRegistration } from './putRegistration';
export { default as updateUserData } from './updateUserData';
export { default as updateUser } from './putUser';
export * from './types';
6 changes: 3 additions & 3 deletions packages/api/src/postUserToGroups.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { PostUserToGroupsRequest, PostUserToGroupsResponse } from './types';
import { PostUsersToGroupsRequest, PostUsersToGroupsResponse } from './types';

async function postUserToGroups({
secret,
groupId,
}: PostUserToGroupsRequest): Promise<PostUserToGroupsResponse | null> {
}: PostUsersToGroupsRequest): Promise<PostUsersToGroupsResponse | null> {
try {
const response = await fetch(`${import.meta.env.VITE_SERVER_URL}/api/users-to-groups`, {
method: 'POST',
Expand All @@ -18,7 +18,7 @@ async function postUserToGroups({
throw new Error(`HTTP Error! Status: ${response.status}`);
}

const group = (await response.json()) as { data: PostUserToGroupsResponse };
const group = (await response.json()) as { data: PostUsersToGroupsResponse };
return group.data;
} catch (error) {
console.error('Error during POST user to groups request:', error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { PutUserRequest, GetUserResponse } from './types/UserType';
async function updateUserData({
email,
firstName,
groupIds,
lastName,
telegram,
userAttributes,
Expand All @@ -20,7 +19,6 @@ async function updateUserData({
body: JSON.stringify({
email,
firstName,
groupIds,
lastName,
telegram,
userAttributes,
Expand Down
35 changes: 35 additions & 0 deletions packages/api/src/putUsersToGroups.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { PostUsersToGroupsResponse, PutUsersToGroupsRequest } from './types';

async function postUserToGroups({
groupId,
userToGroupId,
}: PutUsersToGroupsRequest): Promise<PostUsersToGroupsResponse | null> {
try {
const response = await fetch(
`${import.meta.env.VITE_SERVER_URL}/api/users-to-groups/${userToGroupId}`,
{
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ groupId }),
},
);

if (!response.ok) {
throw new Error(`HTTP Error! Status: ${response.status}`);
}

const group = (await response.json()) as { data: PostUsersToGroupsResponse };
return group.data;
} catch (error) {
console.error('Error during POST user to groups request:', error);
if (error instanceof Error) {
throw new Error(error.message);
}
return null;
}
}

export default postUserToGroups;
20 changes: 14 additions & 6 deletions packages/api/src/types/UserToGroupsType.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
export type PostUserToGroupsRequest = {
groupId?: string;
secret?: string;
};

export type PostUserToGroupsResponse = {
type UsersToGroups = {
id: string;
createdAt: Date;
updatedAt: Date;
userId: string;
groupCategoryId: string | null;
groupId: string;
};

export type PostUsersToGroupsRequest = {
groupId?: string;
secret?: string;
};

export type PutUsersToGroupsRequest = {
userToGroupId: string;
groupId: string;
};

export type PostUsersToGroupsResponse = UsersToGroups;
export type PutUsersToGroupsResponse = UsersToGroups;
1 change: 0 additions & 1 deletion packages/api/src/types/UserType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export type PutUserRequest = {
firstName: string;
lastName: string;
email?: string;
groupIds: string[];
telegram?: string | null;
userAttributes: Record<string, string>;
};

0 comments on commit ab16af3

Please sign in to comment.