import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react' import { getConfigValue } from '@brojs/cli' import { keycloak } from '../kc' import { AccessCode, BaseResponse, Course, Lesson, PopulatedCourse, User, UserData, } from '../model' export const api = createApi({ reducerPath: 'auth', baseQuery: fetchBaseQuery({ baseUrl: getConfigValue('journal.back.url'), fetchFn: async ( input: RequestInfo | URL, init?: RequestInit | undefined, ) => { const response = await fetch(input, init) if (response.status === 403) keycloak.login() return response }, headers: { 'Content-Type': 'application/json;charset=utf-8', }, prepareHeaders: (headers) => { headers.set('Authorization', `Bearer ${keycloak.token}`) }, }), tagTypes: ['LessonList', 'CourseList', 'Course'], endpoints: (builder) => ({ coursesList: builder.query, void>({ query: () => '/course/list', providesTags: ['CourseList'], }), createUpdateCourse: builder.mutation< BaseResponse, Partial & Pick >({ query: (course) => ({ url: '/course', method: 'POST', body: course, }), invalidatesTags: ['CourseList'], }), courseAllStudents: builder.query, string>({ query: (courseId) => `/course/students/${courseId}`, }), manualAddStudent: builder.mutation< BaseResponse, { lessonId: string; user: User } >({ query: ({ lessonId, user }) => ({ url: `/lesson/add-student/${lessonId}`, method: 'POST', body: user, }), }), lessonList: builder.query, string>({ query: (courseId) => `/lesson/list/${courseId}`, providesTags: ['LessonList'], }), generateLessons: builder.mutation, string>({ query: (courseId) => `/lesson/${courseId}/ai/generate-lessons`, }), createLesson: builder.mutation< BaseResponse, Partial & Pick & { courseId: string } >({ query: (data) => ({ url: '/lesson', method: 'POST', body: data, }), invalidatesTags: ['LessonList'], }), updateLesson: builder.mutation, Partial & Pick>({ query: (data) => ({ method: 'PUT', url: '/lesson', body: data, }), invalidatesTags: ['LessonList'], }), deleteLesson: builder.mutation({ query: (lessonId) => ({ url: `/lesson/${lessonId}`, method: 'DELETE', }), invalidatesTags: ['LessonList'], }), lessonById: builder.query, string>({ query: (lessonId: string) => `/lesson/${lessonId}`, }), createAccessCode: builder.query< BaseResponse, { lessonId: string } >({ query: ({ lessonId }) => ({ url: '/lesson/access-code', method: 'POST', body: { lessonId }, }), }), getAccess: builder.query< BaseResponse<{ user: UserData; accessCode: AccessCode }>, { accessCode: string } >({ query: ({ accessCode }) => ({ url: `/lesson/access-code/${accessCode}`, method: 'GET', }), }), sendReaction: builder.mutation({ query: ({ lessonId, reaction }) => ({ url: `/lesson/reaction/${lessonId}`, method: 'POST', body: { reaction }, }), }), getCourseById: builder.query({ query: (courseId) => `/course/${courseId}`, transformResponse: (response: BaseResponse) => response.body, providesTags: ['Course'], }), toggleExamWithJury: builder.mutation({ query: (courseId) => ({ url: `/course/toggle-exam-with-jury/${courseId}`, method: 'POST', body: {}, }), invalidatesTags: ['Course'] }) }), })