Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,9 @@
"verified": "Route is available for verified users!"
}
},
"rightsAcsses": {
"conflict": "Admin or author required"
},
"topic": {
"skill": {
"not_found": "Skill not found"
Expand Down
3 changes: 3 additions & 0 deletions public/locales/ru/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,9 @@
"verified": "Доступно только для верифицированных пользователей"
}
},
"rightsAcsses": {
"conflict": "Требуются права администратора или автора"
},
"topic": {
"skill": {
"not_found": "Навык не найден"
Expand Down

This file was deleted.

38 changes: 0 additions & 38 deletions src/features/topics/createTopics/api/__mock__/createTopic.ts

This file was deleted.

86 changes: 86 additions & 0 deletions src/features/topics/createTopics/api/__mock__/createTopicMock.ts
Original file line number Diff line number Diff line change
@@ -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<TopicCreateError>
>(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);
});
2 changes: 1 addition & 1 deletion src/features/topics/createTopics/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export const getCreateTopicsApiErrorMessage = (error: ApiErrorData<TopicCreateEr
case 'auth.auth.unauthorized':
return i18n.t(Translation.TOAST_TOPIC_CREATE_AUTH_UNAUTHORIZED);

case 'auth.roles.admin_or_author_required':
return i18n.t(Translation.TOAST_TOPIC_CREATE_AUTH_UNAUTHORIZED);

case 'auth.user.verified':
return i18n.t(Translation.TOAST_TOPIC_CREATE_AUTH_USER_VERIFIED);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export type TopicCreateError =
| 'topic.topic.title.conflict'
| 'tinify.tinify.compress_failed'
| 'tinify.tinify.resize_failed'
| 'toast.topics.create.failed';
| 'toast.topics.create.failed'
| 'auth.roles.admin_or_author_required';
Comment thread
PerelomaDenis marked this conversation as resolved.
1 change: 1 addition & 0 deletions src/shared/config/i18n/i18nTranslations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ export enum Translation {
TOAST_TOPIC_CREATE_SUCCESS = 'toast.topics.create.success',
TOAST_TOPIC_CREATE_FAILED = 'toast.topics.create.failed',
TOAST_TOPIC_CREATE_AUTH_UNAUTHORIZED = 'toast.topics.create.auth.unauthorized',
TOAST_TOPIC_ADMIN_OR_AUTHOR_REQUIRED = 'toast.topics.rightsAcsses.conflict',
TOAST_TOPIC_CREATE_AUTH_USER_VERIFIED = 'toast.topics.create.auth.user.verified',
TOAST_TOPIC_CREATE_TOPIC_SKILL_NOT_FOUND = 'toast.topics.create.topic.skill.not_found',
TOAST_TOPIC_CREATE_TOPIC_TITLE_CONFLICT = 'toast.topics.create.topic.title.conflict',
Expand Down
Loading