feat: use session access_tokens

This commit is contained in:
swve 2024-06-01 12:23:01 +01:00
parent 08cc97f557
commit 52f2235942
74 changed files with 413 additions and 440 deletions

View file

@ -1,9 +1,10 @@
import { getAPIUrl } from '@services/config/config'
import { RequestBody } from '@services/utils/ts/requests'
import { RequestBodyWithAuthHeader } from '@services/utils/ts/requests'
export async function startActivityAIChatSession(
message: string,
activity_uuid: string
access_token: string,
activity_uuid?: string
) {
const data = {
message,
@ -11,7 +12,7 @@ export async function startActivityAIChatSession(
}
const result = await fetch(
`${getAPIUrl()}ai/start/activity_chat_session`,
RequestBody('POST', data, null)
RequestBodyWithAuthHeader('POST', data, null, access_token)
)
const json = await result.json()
if (result.status === 200) {
@ -34,7 +35,8 @@ export async function startActivityAIChatSession(
export async function sendActivityAIChatMessage(
message: string,
aichat_uuid: string,
activity_uuid: string
activity_uuid: string,
access_token: string
) {
const data = {
aichat_uuid,
@ -43,7 +45,7 @@ export async function sendActivityAIChatMessage(
}
const result = await fetch(
`${getAPIUrl()}ai/send/activity_chat_message`,
RequestBody('POST', data, null)
RequestBodyWithAuthHeader('POST', data, null, access_token)
)
const json = await result.json()

View file

@ -1,7 +1,16 @@
import { getAPIUrl } from '@services/config/config'
import { RequestBody, RequestBodyForm } from '@services/utils/ts/requests'
import {
RequestBody,
RequestBodyForm,
RequestBodyFormWithAuthHeader,
RequestBodyWithAuthHeader,
} from '@services/utils/ts/requests'
export async function uploadNewImageFile(file: any, activity_uuid: string) {
export async function uploadNewImageFile(
file: any,
activity_uuid: string,
access_token: string
) {
// Send file thumbnail as form data
const formData = new FormData()
formData.append('file_object', file)
@ -9,17 +18,17 @@ export async function uploadNewImageFile(file: any, activity_uuid: string) {
return fetch(
`${getAPIUrl()}blocks/image`,
RequestBodyForm('POST', formData, null)
RequestBodyFormWithAuthHeader('POST', formData, null, access_token)
)
.then((result) => result.json())
.catch((error) => console.log('error', error))
}
export async function getImageFile(file_id: string) {
export async function getImageFile(file_id: string, access_token: string) {
// todo : add course id to url
return fetch(
`${getAPIUrl()}blocks/image?file_id=${file_id}`,
RequestBody('GET', null, null)
RequestBodyWithAuthHeader('GET', null, null, access_token)
)
.then((result) => result.json())
.catch((error) => console.log('error', error))

View file

@ -1,7 +1,16 @@
import { getAPIUrl } from '@services/config/config'
import { RequestBody, RequestBodyForm } from '@services/utils/ts/requests'
import {
RequestBody,
RequestBodyForm,
RequestBodyFormWithAuthHeader,
RequestBodyWithAuthHeader,
} from '@services/utils/ts/requests'
export async function uploadNewPDFFile(file: any, activity_uuid: string) {
export async function uploadNewPDFFile(
file: any,
activity_uuid: string,
access_token: string
) {
// Send file thumbnail as form data
const formData = new FormData()
formData.append('file_object', file)
@ -9,17 +18,17 @@ export async function uploadNewPDFFile(file: any, activity_uuid: string) {
return fetch(
`${getAPIUrl()}blocks/pdf`,
RequestBodyForm('POST', formData, null)
RequestBodyFormWithAuthHeader('POST', formData, null, access_token)
)
.then((result) => result.json())
.catch((error) => console.log('error', error))
}
export async function getPDFFile(file_id: string) {
export async function getPDFFile(file_id: string, access_token: string) {
// todo : add course id to url
return fetch(
`${getAPIUrl()}blocks/pdf?file_id=${file_id}`,
RequestBody('GET', null, null)
RequestBodyWithAuthHeader('GET', null, null, access_token)
)
.then((result) => result.json())
.catch((error) => console.log('error', error))

View file

@ -1,10 +1,10 @@
import { getAPIUrl } from '@services/config/config'
import { RequestBody } from '@services/utils/ts/requests'
import { RequestBody, RequestBodyWithAuthHeader } from '@services/utils/ts/requests'
export async function submitQuizBlock(activity_id: string, data: any) {
export async function submitQuizBlock(activity_id: string, data: any,access_token:string) {
const result: any = await fetch(
`${getAPIUrl()}blocks/quiz/${activity_id}"`,
RequestBody('POST', data, null)
RequestBodyWithAuthHeader('POST', data, null,access_token)
)
.then((result) => result.json())
.catch((error) => console.log('error', error))

View file

@ -1,7 +1,16 @@
import { getAPIUrl } from '@services/config/config'
import { RequestBody, RequestBodyForm } from '@services/utils/ts/requests'
import {
RequestBody,
RequestBodyForm,
RequestBodyFormWithAuthHeader,
RequestBodyWithAuthHeader,
} from '@services/utils/ts/requests'
export async function uploadNewVideoFile(file: any, activity_uuid: string) {
export async function uploadNewVideoFile(
file: any,
activity_uuid: string,
access_token: string
) {
// Send file thumbnail as form data
const formData = new FormData()
formData.append('file_object', file)
@ -9,17 +18,17 @@ export async function uploadNewVideoFile(file: any, activity_uuid: string) {
return fetch(
`${getAPIUrl()}blocks/video`,
RequestBodyForm('POST', formData, null)
RequestBodyFormWithAuthHeader('POST', formData, null, access_token)
)
.then((result) => result.json())
.catch((error) => console.log('error', error))
.catch((error) => console.error('error', error))
}
export async function getVideoFile(file_id: string) {
export async function getVideoFile(file_id: string, access_token: string) {
return fetch(
`${getAPIUrl()}blocks/video?file_id=${file_id}`,
RequestBody('GET', null, null)
RequestBodyWithAuthHeader('GET', null, null, access_token)
)
.then((result) => result.json())
.catch((error) => console.log('error', error))
.catch((error) => console.error('error', error))
}

View file

@ -1,4 +1,4 @@
import { RequestBody, RequestBodyWithAuthHeader, errorHandling } from '@services/utils/ts/requests'
import { RequestBodyWithAuthHeader, errorHandling } from '@services/utils/ts/requests'
import { getAPIUrl } from '@services/config/config'
/*

View file

@ -23,7 +23,6 @@ export async function deleteCollection(
// Create a new collection
export async function createCollection(collection: any, access_token: any) {
console.log(collection)
const result: any = await fetch(
`${getAPIUrl()}collections/`,
RequestBodyWithAuthHeader('POST', collection, null, access_token)

View file

@ -1,7 +1,5 @@
import { getAPIUrl } from '@services/config/config'
import {
RequestBody,
RequestBodyForm,
RequestBodyFormWithAuthHeader,
RequestBodyWithAuthHeader,
errorHandling,

View file

@ -1,6 +1,5 @@
import { getAPIUrl } from '@services/config/config'
import {
RequestBody,
RequestBodyWithAuthHeader,
getResponseMetadata,
} from '@services/utils/ts/requests'

View file

@ -1,6 +1,5 @@
import { getAPIUrl } from '@services/config/config'
import {
RequestBody,
RequestBodyWithAuthHeader,
getResponseMetadata,
} from '@services/utils/ts/requests'

View file

@ -1,6 +1,6 @@
import { getAPIUrl } from '@services/config/config'
import {
RequestBody,
RequestBodyWithAuthHeader,
errorHandling,
getResponseMetadata,
} from '@services/utils/ts/requests'
@ -10,37 +10,48 @@ import {
GET requests are called from the frontend using SWR (https://swr.vercel.app/)
*/
export async function createNewOrganization(body: any) {
export async function createNewOrganization(body: any, access_token: string) {
const result = await fetch(
`${getAPIUrl()}orgs/`,
RequestBody('POST', body, null)
RequestBodyWithAuthHeader('POST', body, null,access_token)
)
const res = await errorHandling(result)
return res
}
export async function deleteOrganizationFromBackend(org_id: any) {
export async function deleteOrganizationFromBackend(
org_id: any,
access_token: string
) {
const result = await fetch(
`${getAPIUrl()}orgs/${org_id}`,
RequestBody('DELETE', null, null)
RequestBodyWithAuthHeader('DELETE', null, null, access_token)
)
const res = await errorHandling(result)
return res
}
export async function getOrganizationContextInfo(org_slug: any, next: any) {
export async function getOrganizationContextInfo(
org_slug: any,
next: any,
access_token?: string
) {
const result = await fetch(
`${getAPIUrl()}orgs/slug/${org_slug}`,
RequestBody('GET', null, next)
RequestBodyWithAuthHeader('GET', null, next, access_token)
)
const res = await errorHandling(result)
return res
}
export async function getOrganizationContextInfoWithId(org_id: any, next: any) {
export async function getOrganizationContextInfoWithId(
org_id: any,
next: any,
access_token: string
) {
const result = await fetch(
`${getAPIUrl()}orgs/${org_id}`,
RequestBody('GET', null, next)
RequestBodyWithAuthHeader('GET', null, next, access_token)
)
const res = await errorHandling(result)
return res
@ -64,10 +75,14 @@ export async function getOrganizationContextInfoWithoutCredentials(
return res
}
export function getOrganizationContextInfoNoAsync(org_slug: any, next: any) {
export function getOrganizationContextInfoNoAsync(
org_slug: any,
next: any,
access_token: string
) {
const result = fetch(
`${getAPIUrl()}orgs/slug/${org_slug}`,
RequestBody('GET', null, next)
RequestBodyWithAuthHeader('GET', null, next, access_token)
)
return result
}
@ -75,20 +90,25 @@ export function getOrganizationContextInfoNoAsync(org_slug: any, next: any) {
export async function updateUserRole(
org_id: any,
user_id: any,
role_uuid: any
role_uuid: any,
access_token: string
) {
const result = await fetch(
`${getAPIUrl()}orgs/${org_id}/users/${user_id}/role/${role_uuid}`,
RequestBody('PUT', null, null)
RequestBodyWithAuthHeader('PUT', null, null, access_token)
)
const res = await getResponseMetadata(result)
return res
}
export async function removeUserFromOrg(org_id: any, user_id: any) {
export async function removeUserFromOrg(
org_id: any,
user_id: any,
access_token: any
) {
const result = await fetch(
`${getAPIUrl()}orgs/${org_id}/users/${user_id}`,
RequestBody('DELETE', null, null)
RequestBodyWithAuthHeader('DELETE', null, null, access_token)
)
const res = await getResponseMetadata(result)
return res

View file

@ -1,8 +1,8 @@
import { getAPIUrl } from '@services/config/config'
import {
RequestBody,
errorHandling,
RequestBodyForm,
RequestBodyWithAuthHeader,
RequestBodyFormWithAuthHeader,
} from '@services/utils/ts/requests'
/*
@ -10,22 +10,30 @@ import {
GET requests are called from the frontend using SWR (https://swr.vercel.app/)
*/
export async function updateOrganization(org_id: string, data: any) {
export async function updateOrganization(
org_id: string,
data: any,
access_token: string
) {
const result: any = await fetch(
`${getAPIUrl()}orgs/` + org_id,
RequestBody('PUT', data, null)
RequestBodyWithAuthHeader('PUT', data, null, access_token)
)
const res = await errorHandling(result)
return res
}
export async function uploadOrganizationLogo(org_id: string, logo_file: any) {
export async function uploadOrganizationLogo(
org_id: string,
logo_file: any,
access_token: string
) {
// Send file thumbnail as form data
const formData = new FormData()
formData.append('logo_file', logo_file)
const result: any = await fetch(
`${getAPIUrl()}orgs/` + org_id + '/logo',
RequestBodyForm('PUT', formData, null)
RequestBodyFormWithAuthHeader('PUT', formData, null, access_token)
)
const res = await errorHandling(result)
return res

View file

@ -1,15 +1,22 @@
import { getAPIUrl } from '@services/config/config'
import { RequestBody, errorHandling } from '@services/utils/ts/requests'
import {
RequestBodyWithAuthHeader,
errorHandling,
} from '@services/utils/ts/requests'
/*
This file includes only POST, PUT, DELETE requests
GET requests are called from the frontend using SWR (https://swr.vercel.app/)
*/
export async function updatePassword(user_id: string, data: any) {
export async function updatePassword(
user_id: string,
data: any,
access_token: any
) {
const result: any = await fetch(
`${getAPIUrl()}users/change_password/` + user_id,
RequestBody('PUT', data, null)
RequestBodyWithAuthHeader('PUT', data, null, access_token)
)
const res = await errorHandling(result)
return res

View file

@ -1,15 +1,22 @@
import { getAPIUrl } from '@services/config/config'
import { RequestBody, errorHandling } from '@services/utils/ts/requests'
import {
RequestBodyWithAuthHeader,
errorHandling,
} from '@services/utils/ts/requests'
/*
This file includes only POST, PUT, DELETE requests
GET requests are called from the frontend using SWR (https://swr.vercel.app/)
*/
export async function updateProfile(data: any, user_id: number) {
export async function updateProfile(
data: any,
user_id: number,
access_token: string
) {
const result: any = await fetch(
`${getAPIUrl()}users/` + user_id,
RequestBody('PUT', data, null)
RequestBodyWithAuthHeader('PUT', data, null, access_token)
)
const res = await errorHandling(result)
return res

View file

@ -1,46 +1,60 @@
import { getAPIUrl } from '@services/config/config'
import { RequestBody, getResponseMetadata } from '@services/utils/ts/requests'
import {
RequestBodyWithAuthHeader,
getResponseMetadata,
} from '@services/utils/ts/requests'
export async function getUserGroups(org_id: any) {
export async function getUserGroups(org_id: any, access_token: string) {
const result: any = await fetch(
`${getAPIUrl()}usergroups/org/${org_id}`,
RequestBody('GET', null, null)
RequestBodyWithAuthHeader('GET', null, null, access_token)
)
const res = await getResponseMetadata(result)
return res
}
export async function createUserGroup(body: any) {
export async function createUserGroup(body: any, access_token: string) {
const result: any = await fetch(
`${getAPIUrl()}usergroups/`,
RequestBody('POST', body, null)
RequestBodyWithAuthHeader('POST', body, null, access_token)
)
const res = await getResponseMetadata(result)
return res
}
export async function linkUserToUserGroup(usergroup_id: any, user_id: any) {
export async function linkUserToUserGroup(
usergroup_id: any,
user_id: any,
access_token: string
) {
const result: any = await fetch(
`${getAPIUrl()}usergroups/${usergroup_id}/add_users?user_ids=${user_id}`,
RequestBody('POST', null, null)
RequestBodyWithAuthHeader('POST', null, null, access_token)
)
const res = await getResponseMetadata(result)
return res
}
export async function unLinkUserToUserGroup(usergroup_id: any, user_id: any) {
export async function unLinkUserToUserGroup(
usergroup_id: any,
user_id: any,
access_token: string
) {
const result: any = await fetch(
`${getAPIUrl()}usergroups/${usergroup_id}/remove_users?user_ids=${user_id}`,
RequestBody('DELETE', null, null)
RequestBodyWithAuthHeader('DELETE', null, null, access_token)
)
const res = await getResponseMetadata(result)
return res
}
export async function deleteUserGroup(usergroup_id: number) {
export async function deleteUserGroup(
usergroup_id: number,
access_token: string
) {
const result: any = await fetch(
`${getAPIUrl()}usergroups/${usergroup_id}`,
RequestBody('DELETE', null, null)
RequestBodyWithAuthHeader('DELETE', null, null, access_token)
)
const res = await getResponseMetadata(result)
return res
@ -48,11 +62,12 @@ export async function deleteUserGroup(usergroup_id: number) {
export async function linkResourcesToUserGroup(
usergroup_id: any,
resource_uuids: any
resource_uuids: any,
access_token: string
) {
const result: any = await fetch(
`${getAPIUrl()}usergroups/${usergroup_id}/add_resources?resource_uuids=${resource_uuids}`,
RequestBody('POST', null, null)
RequestBodyWithAuthHeader('POST', null, null, access_token)
)
const res = await getResponseMetadata(result)
return res
@ -60,11 +75,12 @@ export async function linkResourcesToUserGroup(
export async function unLinkResourcesToUserGroup(
usergroup_id: any,
resource_uuids: any
resource_uuids: any,
access_token: string
) {
const result: any = await fetch(
`${getAPIUrl()}usergroups/${usergroup_id}/remove_resources?resource_uuids=${resource_uuids}`,
RequestBody('DELETE', null, null)
RequestBodyWithAuthHeader('DELETE', null, null, access_token)
)
const res = await getResponseMetadata(result)
return res

View file

@ -1,7 +1,7 @@
import { getAPIUrl } from '@services/config/config'
import {
RequestBody,
RequestBodyForm,
RequestBodyFormWithAuthHeader,
errorHandling,
getResponseMetadata,
} from '@services/utils/ts/requests'
@ -15,12 +15,16 @@ export async function getUser(user_id: string) {
return res
}
export async function updateUserAvatar(user_uuid: any, avatar_file: any) {
export async function updateUserAvatar(
user_uuid: any,
avatar_file: any,
access_token: any
) {
const formData = new FormData()
formData.append('avatar_file', avatar_file)
const result: any = await fetch(
`${getAPIUrl()}users/update_avatar/${user_uuid}`,
RequestBodyForm('PUT', formData, null)
RequestBodyFormWithAuthHeader('PUT', formData, null, access_token)
)
const res = await getResponseMetadata(result)
return res

View file

@ -20,7 +20,7 @@ export const RequestBodyWithAuthHeader = (
method: string,
data: any,
next: any,
token: string
token?: string
) => {
let HeadersConfig = new Headers(
token