Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete user from project #750

Merged
merged 9 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 13 additions & 2 deletions components/ProjectEdit/Participants/Participants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useRouter } from 'next/router'

import axios from 'axios'
import { useTranslation } from 'next-i18next'
import toast from 'react-hot-toast'

import Modal from 'components/Modal'

Pavel064 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -90,14 +91,24 @@ function Parcticipants({ users, access: { isCoordinatorAccess, isAdminAccess } }
}
}, [translators])
const remove = (userId, role) => {
axios
return axios
.delete(`/api/projects/${code}/${role}/${userId}`)
.then(() => {
roleActions[role].reset(false)
roleActions[role].mutate()
})
.catch(console.log)
.catch((error) => {
if (
error.response?.data?.error === 'Cannot remove translator with assigned verses'
) {
toast.error(t('project-edit:CannotRemoveTranslatorWithVerses'))
} else {
toast.error(t('common:SomethingWentWrong'))
}
throw error
})
}

return (
<>
<div className="hidden divide-y divide-th-text-primary sm:block">
Expand Down
1 change: 0 additions & 1 deletion components/SideBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,6 @@ function SideBar({ setIsOpenSideBar, access, isOpenSideBar }) {
className="flex w-full cursor-pointer items-center gap-2"
onClick={() => {
openModal('tAcademy')
setShowAbout(false)
}}
>
<div className="rounded-[23rem]">
Expand Down
41 changes: 27 additions & 14 deletions pages/api/projects/[code]/translators/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,48 @@ export default async function languageProjectTranslatorHandler(req, res) {
method,
} = req
let project_id = null

switch (method) {
case 'DELETE':
try {
const { data: project, error } = await supabase
.from('projects')
.select('id, code')
.select('id')
.eq('code', code)
.limit(1)
.maybeSingle()
.single()
if (error) throw error
if (project?.id) {
project_id = project?.id
} else {
throw { error: 'Missing id of project' }
if (!project?.id) throw { error: 'Missing id of project' }

const { data: translator, error: translatorError } = await supabase
.from('project_translators')
.select('id')
.match({ project_id: project.id, user_id: id })
.single()
if (translatorError) throw translatorError

const { data: hasVerses, error: checkError } = await supabase.rpc(
'has_assigned_verses',
{ project_translator_id: translator.id }
)
if (checkError) throw checkError
if (hasVerses) {
return res.status(400).json({
error: 'Cannot remove translator with assigned verses',
})
}
} catch (error) {
return res.status(404).json({ error })
}
try {
const { data, error } = await supabase

const { data, error: deleteError } = await supabase
.from('project_translators')
.delete()
.match({ project_id: project_id, user_id: id })
.match({ id: translator.id })
.select()
if (error) throw error
if (deleteError) throw deleteError

return res.status(200).json(data)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Потом где используются эти данные? Их точно нужно передавать?

} catch (error) {
return res.status(404).json({ error })
}

default:
res.setHeader('Allow', ['DELETE'])
return res.status(405).end(`Method ${method} Not Allowed`)
Expand Down
1 change: 1 addition & 0 deletions public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
"ShowAll": "Show all",
"ShowAllUpdates": "Version history",
"ShowCurrUpdates": "Latest version",
"SomethingWentWrong": "Something went wrong. Please try again",
"SomeTranslatorsNotFinish": "Some translators are still working on the previous step. You will now be automatically redirected.",
"Start": "Start",
"Step": "Step",
Expand Down
1 change: 1 addition & 0 deletions public/locales/en/project-edit.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"BriefToggleError": "Failed to toggle the brief",
"BriefToggleisenableSuccessDisabled": "The brief has been disabled",
"BriefToggleisenableSuccessEnabled": "The brief has been enabled",
"CannotRemoveTranslatorWithVerses": "Cannot remove translator with verses in unfinished chapters. Please reassign verses or finish chapters first",
"CodeLanguageNotUnique": "The code of the language is not unique",
"CodeLanguageRequired": "The code of the language is required",
"CreateNewProject": "Create a new project",
Expand Down
3 changes: 0 additions & 3 deletions supabase/migrations/20240313204519_remote_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1828,9 +1828,6 @@ ALTER TABLE ONLY "public"."languages"
ALTER TABLE ONLY "public"."logs"
ADD CONSTRAINT "logs_pkey" PRIMARY KEY ("id");

ALTER TABLE ONLY "public"."methods"
ADD CONSTRAINT "methods_pkey" PRIMARY KEY ("id");

ALTER TABLE ONLY "public"."personal_notes"
ADD CONSTRAINT "personal_notes_pkey" PRIMARY KEY ("id");

Expand Down
12 changes: 12 additions & 0 deletions supabase/migrations/20241211162405_add_has_assigned_verses.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE OR REPLACE FUNCTION public.has_assigned_verses(project_translator_id bigint)
RETURNS boolean
LANGUAGE plpgsql
AS $function$
BEGIN
RETURN EXISTS (
SELECT 1 FROM verses
WHERE verses.project_translator_id = has_assigned_verses.project_translator_id
);
END;
$function$
;
Loading