diff --git a/public/locales/en/translation.json b/public/locales/en/translation.json index 7c4f6ccfe..ff7a8750d 100644 --- a/public/locales/en/translation.json +++ b/public/locales/en/translation.json @@ -533,6 +533,9 @@ "verified": "Route is available for verified users!" } }, + "rightsAcsses": { + "conflict": "Admin or author required" + }, "topic": { "skill": { "not_found": "Skill not found" diff --git a/public/locales/ru/translation.json b/public/locales/ru/translation.json index 54b7a5f9e..a96624d7a 100644 --- a/public/locales/ru/translation.json +++ b/public/locales/ru/translation.json @@ -536,6 +536,9 @@ "verified": "Доступно только для верифицированных пользователей" } }, + "rightsAcsses": { + "conflict": "Требуются права администратора или автора" + }, "topic": { "skill": { "not_found": "Навык не найден" diff --git a/src/features/specialization/createSpecialization/api/__mock__/createSpecializationErrorMock.ts b/src/features/specialization/createSpecialization/api/__mock__/createSpecializationErrorMock.ts deleted file mode 100644 index 45f9a114c..000000000 --- a/src/features/specialization/createSpecialization/api/__mock__/createSpecializationErrorMock.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { http, HttpResponse } from 'msw'; - -import { createSpecializationApiUrls } from '../../model/constants/createSpecializationConstants'; - -export const createSpecializationErrorMock = http.post( - process.env.API_URL + createSpecializationApiUrls.createSpecialization, - async () => { - return HttpResponse.json( - { - message: 'auth.user.verified', - statusCode: 403, - description: 'Route is available for verified users!', - }, - { status: 403 }, - ); - }, -); diff --git a/src/features/topics/createTopics/api/__mock__/createTopic.ts b/src/features/topics/createTopics/api/__mock__/createTopic.ts deleted file mode 100644 index d8e879114..000000000 --- a/src/features/topics/createTopics/api/__mock__/createTopic.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { http, HttpResponse } from 'msw'; - -import { author } from '@/shared/libs'; - -import { skillsMock } from '@/entities/skill'; -import { topicsMocks } from '@/entities/topic'; - -import { createTopicApiUrls } from '../../model/constants/createTopicConstants'; -import { CreateTopicBodyRequest, CreateTopicResponse } from '../../model/types/topicCreateTypes'; - -export const createTopicMock = http.post( - process.env.API_URL + createTopicApiUrls.createTopic, - async ({ request }) => { - const topic: CreateTopicBodyRequest = await request.json(); - - const response: CreateTopicResponse = { - id: Date.now(), - title: topic.title, - description: topic.description, - imageSrc: null, - createdAt: new Date().toISOString(), - updatedAt: new Date().toISOString(), - skill: skillsMock.find((skill) => skill.id === topic.skillId) ?? { - id: topic.skillId, - title: '', - description: '', - imageSrc: null, - createdAt: new Date().toISOString(), - updatedAt: new Date().toISOString(), - specializations: [], - createdBy: author, - }, - }; - - topicsMocks.data.push(response); - return HttpResponse.json(response); - }, -); diff --git a/src/features/topics/createTopics/api/__mock__/createTopicMock.ts b/src/features/topics/createTopics/api/__mock__/createTopicMock.ts new file mode 100644 index 000000000..c2f72549b --- /dev/null +++ b/src/features/topics/createTopics/api/__mock__/createTopicMock.ts @@ -0,0 +1,86 @@ +import { http, HttpResponse } from 'msw'; + +import { getMockAuthProfile, listAdminRoles } from '@/entities/auth'; +import { skillsMock } from '@/entities/skill'; +import { topicsMocks } from '@/entities/topic'; + +import { createTopicApiUrls } from '../../model/constants/createTopicConstants'; +import { TopicCreateError } from '../../model/types/topicCreateErrorTypes'; +import { CreateTopicBodyRequest, CreateTopicResponse } from '../../model/types/topicCreateTypes'; + +export const createTopicMock = http.post< + never, + CreateTopicBodyRequest, + CreateTopicResponse | ApiErrorData +>(process.env.API_URL + createTopicApiUrls.createTopic, async ({ request }) => { + const profileMockResponse = getMockAuthProfile(request); + + if (!profileMockResponse) { + return HttpResponse.json( + { + message: 'auth.auth.unauthorized', + statusCode: 401, + description: 'Authentication failed', + }, + { status: 401 }, + ); + } + if (!profileMockResponse.isVerified) { + return HttpResponse.json( + { + message: 'auth.user.verified', + statusCode: 403, + description: 'Route is available for verified users!', + }, + { status: 403 }, + ); + } + + if (!profileMockResponse.userRoles.some((role) => listAdminRoles.includes(role.name))) { + return HttpResponse.json( + { + message: 'auth.roles.admin_or_author_required', + statusCode: 403, + description: 'Admin or author required', + }, + { status: 403 }, + ); + } + + const topic: CreateTopicBodyRequest = await request.json(); + + if (topicsMocks.data.some((t) => t.title === topic.title && t.skill.id === topic.skillId)) { + return HttpResponse.json( + { + message: 'topic.topic.title.conflict', + statusCode: 409, + description: 'A topic with the same title already exists for this skill', + }, + { status: 409 }, + ); + } + + const date = new Date().toISOString(); + + const response: CreateTopicResponse = { + id: Date.now(), + title: topic.title, + description: topic.description, + imageSrc: null, + createdAt: date, + updatedAt: date, + skill: skillsMock.find((skill) => skill.id === topic.skillId) ?? { + id: topic.skillId, + title: '', + description: '', + imageSrc: null, + createdAt: date, + updatedAt: date, + specializations: [], + createdBy: { id: '', username: '' }, + }, + }; + + topicsMocks.data.push(response); + return HttpResponse.json(response); +}); diff --git a/src/features/topics/createTopics/index.ts b/src/features/topics/createTopics/index.ts index ba9b1ceb8..cda8eda1d 100644 --- a/src/features/topics/createTopics/index.ts +++ b/src/features/topics/createTopics/index.ts @@ -1,4 +1,4 @@ -import { createTopicMock } from './api/__mock__/createTopic'; +import { createTopicMock } from './api/__mock__/createTopicMock'; export { useCreateTopicMutation } from './api/createTopicApi'; export { TopicCreateForm } from './ui/TopicCreateForm/TopicCreateForm'; diff --git a/src/features/topics/createTopics/lib/utils/getCreateTopicsApiErrorMessage.ts b/src/features/topics/createTopics/lib/utils/getCreateTopicsApiErrorMessage.ts index 4a0b4d277..a1e1ac39a 100644 --- a/src/features/topics/createTopics/lib/utils/getCreateTopicsApiErrorMessage.ts +++ b/src/features/topics/createTopics/lib/utils/getCreateTopicsApiErrorMessage.ts @@ -7,6 +7,9 @@ export const getCreateTopicsApiErrorMessage = (error: ApiErrorData