-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Lbac 1734 securisation de /user/:userId (#802)
* fix: extraction de la fonction getUser dans l'api client * fix: typing dans UserValidationHistory * fix: changement de permission + extraction de updateUser * fix: permission
- Loading branch information
1 parent
454625b
commit c10da68
Showing
8 changed files
with
96 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,94 @@ | ||
import { Badge, Box, Table, TableContainer, Tbody, Td, Text, Th, Thead, Tr } from "@chakra-ui/react" | ||
import dayjs from "dayjs" | ||
import { memo, useCallback, useEffect, useState } from "react" | ||
import { useCallback, useEffect, useState } from "react" | ||
import { IUserStatusValidation } from "shared" | ||
|
||
import { apiGet } from "@/utils/api.utils" | ||
import { getUser } from "@/utils/api" | ||
|
||
import LoadingEmptySpace from "./LoadingEmptySpace" | ||
|
||
// eslint-disable-next-line react/display-name | ||
export default memo( | ||
({ | ||
// @ts-expect-error: TODO | ||
histories, | ||
}) => { | ||
const [historic, setHistoric] = useState([]) | ||
export const UserValidationHistory = ({ histories }: { histories: IUserStatusValidation[] }) => { | ||
const [historic, setHistoric] = useState<(IUserStatusValidation & { first_name?: string; last_name?: string })[]>([]) | ||
|
||
const getValidator = useCallback(async () => { | ||
const buffer = await Promise.all( | ||
histories.map(async (user) => { | ||
if (user.user !== "SERVEUR") { | ||
try { | ||
const result = await apiGet(`/user/:userId`, { params: { userId: user.user } }) | ||
user.first_name = result.first_name | ||
user.last_name = result.last_name | ||
return user | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
const getValidator = useCallback(async () => { | ||
const buffer = await Promise.all( | ||
histories.map(async (statusChange) => { | ||
if (statusChange.user !== "SERVEUR") { | ||
try { | ||
const result = await getUser(statusChange.user) | ||
const { first_name, last_name } = result | ||
return { ...statusChange, first_name, last_name } | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
} | ||
return statusChange | ||
}) | ||
) | ||
setHistoric(buffer) | ||
}, []) | ||
|
||
return user | ||
}) | ||
) | ||
setHistoric(buffer) | ||
}, []) | ||
|
||
useEffect(() => { | ||
getValidator() | ||
}, [historic.length > 0, histories]) | ||
useEffect(() => { | ||
getValidator() | ||
}, [historic.length > 0, histories]) | ||
|
||
if (historic.length === 0) { | ||
return <LoadingEmptySpace /> | ||
} | ||
if (historic.length === 0) { | ||
return <LoadingEmptySpace /> | ||
} | ||
|
||
const getStatut = (status) => { | ||
switch (status) { | ||
case "VALIDÉ": | ||
return <Badge variant="active">{status}</Badge> | ||
case "EN ATTENTE DE VALIDATION": | ||
return <Badge variant="awaiting">{status}</Badge> | ||
case "DESACTIVÉ": | ||
return <Badge variant="inactive">{status}</Badge> | ||
default: | ||
return <Badge>{status}</Badge> | ||
} | ||
const getStatut = (status) => { | ||
switch (status) { | ||
case "VALIDÉ": | ||
return <Badge variant="active">{status}</Badge> | ||
case "EN ATTENTE DE VALIDATION": | ||
return <Badge variant="awaiting">{status}</Badge> | ||
case "DESACTIVÉ": | ||
return <Badge variant="inactive">{status}</Badge> | ||
default: | ||
return <Badge>{status}</Badge> | ||
} | ||
} | ||
|
||
return ( | ||
<Box mt={10}> | ||
<hr /> | ||
<Box mt={5}> | ||
<Text fontSize="20px" fontWeight={700}> | ||
Historique du compte | ||
</Text> | ||
<TableContainer mt={4}> | ||
<Table variant="simple"> | ||
<Thead> | ||
<Tr> | ||
<Th>#</Th> | ||
<Th>Date</Th> | ||
<Th>Statut</Th> | ||
<Th>Type de validation</Th> | ||
<Th>Opérateur</Th> | ||
<Th>Motif</Th> | ||
</Tr> | ||
</Thead> | ||
<Tbody> | ||
{historic | ||
.map(({ date, status, first_name, last_name, validation_type, reason, user }, i) => { | ||
return ( | ||
<Tr key={i}> | ||
<Td>{i + 1}</Td> | ||
<Td>{dayjs(date).format("DD/MM/YYYY")}</Td> | ||
<Td>{getStatut(status)}</Td> | ||
<Td>{validation_type}</Td> | ||
<Td>{first_name && last_name ? `${first_name} ${last_name}` : <Badge>{user}</Badge>}</Td> | ||
<Td>{reason}</Td> | ||
</Tr> | ||
) | ||
}) | ||
.reverse()} | ||
</Tbody> | ||
</Table> | ||
</TableContainer> | ||
</Box> | ||
return ( | ||
<Box mt={10}> | ||
<hr /> | ||
<Box mt={5}> | ||
<Text fontSize="20px" fontWeight={700}> | ||
Historique du compte | ||
</Text> | ||
<TableContainer mt={4}> | ||
<Table variant="simple"> | ||
<Thead> | ||
<Tr> | ||
<Th>#</Th> | ||
<Th>Date</Th> | ||
<Th>Statut</Th> | ||
<Th>Type de validation</Th> | ||
<Th>Opérateur</Th> | ||
<Th>Motif</Th> | ||
</Tr> | ||
</Thead> | ||
<Tbody> | ||
{historic | ||
.map(({ date, status, first_name, last_name, validation_type, reason, user }, i) => { | ||
return ( | ||
<Tr key={i}> | ||
<Td>{i + 1}</Td> | ||
<Td>{dayjs(date).format("DD/MM/YYYY")}</Td> | ||
<Td>{getStatut(status)}</Td> | ||
<Td>{validation_type}</Td> | ||
<Td>{first_name && last_name ? `${first_name} ${last_name}` : <Badge>{user}</Badge>}</Td> | ||
<Td>{reason}</Td> | ||
</Tr> | ||
) | ||
}) | ||
.reverse()} | ||
</Tbody> | ||
</Table> | ||
</TableContainer> | ||
</Box> | ||
) | ||
} | ||
) | ||
</Box> | ||
) | ||
} | ||
|
||
export default UserValidationHistory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters