Skip to content

Commit

Permalink
feat: see docs on interface sdp (#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
YoannNumericite authored Feb 3, 2023
1 parent 8652783 commit 6029ef5
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 48 deletions.
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged

45 changes: 44 additions & 1 deletion formulaire/pages/api/dossier/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ function getId(req: NextApiRequest): number {
}

const get: NextApiHandler = async (req, res) => {

const session = await getSession({ req });
const prisma = new PrismaClient()

var jwt = require('jsonwebtoken');

try {
const dossierId = getId(req);
const dossier = await prisma.dossier.findUnique({
Expand All @@ -51,7 +55,46 @@ function getId(req: NextApiRequest): number {
where: { id: dossierId }
});
if(dossier?.userId === session?.dbUser.id) {
res.status(200).json(dossier);
res.status(200).json({dossier: dossier, docs: {
dossier: {
id: dossier?.id,
piecesDossier: dossier?.piecesDossier.map((piece) => {
let payload = {
iat: new Date().getTime() / 1000,
id: piece.id,
dossierId: dossier.id,
path: piece.link,
}
let tokenSDP = jwt.sign({...payload}, process.env.SECRET_KEY_DOCS, { expiresIn: 60 * 30 });
return {
id: piece.id,
type: piece.type,
statut: piece.statut,
link: `${process.env.NEXTAUTH_URL}/docs?token=${tokenSDP}`
}
})
},
enfants: dossier?.enfants.map((enfant) => {
return {
id: enfant.id,
piecesDossier: enfant.piecesDossier.map((piece) => {
let payload = {
iat: new Date().getTime() / 1000,
id: piece.id,
dossierId: dossier.id,
path: piece.link,
}
let tokenSDP = jwt.sign({...payload}, process.env.SECRET_KEY_DOCS, { expiresIn: 60 * 30 });
return {
id: piece.id,
type: piece.type,
statut: piece.statut,
link: `${process.env.NEXTAUTH_URL}/docs?token=${tokenSDP}`
}
})
}
})
}});
} else {
res.status(401).json({message: 'Unauthorized'})
}
Expand Down
51 changes: 27 additions & 24 deletions formulaire/pages/api/sync/inc/docs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,11 @@ const get: NextApiHandler = async (req, res) => {
}
})
res.status(200).json({
dossier: {
id: dossier?.id,
piecesDossier: dossier?.piecesDossier.map((piece) => {
let payload = {
iat: new Date().getTime() / 1000,
id: piece.id,
dossierId: dossier.id,
path: piece.link,
}
let tokenSDP = jwt.sign({...payload}, process.env.SECRET_KEY_DOCS, { expiresIn: 60 * 30 });
return {
id: piece.id,
type: piece.type,
statut: piece.statut,
link: `${process.env.NEXTAUTH_URL}/docs?token=${tokenSDP}`
}
})
},
enfants: dossier?.enfants.map((enfant) => {
return {
id: enfant.id,
piecesDossier: enfant.piecesDossier.map((piece) => {
...dossier,
docs: {
dossier: {
id: dossier?.id,
piecesDossier: dossier?.piecesDossier.map((piece) => {
let payload = {
iat: new Date().getTime() / 1000,
id: piece.id,
Expand All @@ -70,8 +53,28 @@ const get: NextApiHandler = async (req, res) => {
link: `${process.env.NEXTAUTH_URL}/docs?token=${tokenSDP}`
}
})
}
})
},
enfants: dossier?.enfants.map((enfant) => {
return {
id: enfant.id,
piecesDossier: enfant.piecesDossier.map((piece) => {
let payload = {
iat: new Date().getTime() / 1000,
id: piece.id,
dossierId: dossier.id,
path: piece.link,
}
let tokenSDP = jwt.sign({...payload}, process.env.SECRET_KEY_DOCS, { expiresIn: 60 * 30 });
return {
id: piece.id,
type: piece.type,
statut: piece.statut,
link: `${process.env.NEXTAUTH_URL}/docs?token=${tokenSDP}`
}
})
}
})
}
});
}
};
Expand Down
1 change: 1 addition & 0 deletions formulaire/pages/docs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const getServerSideProps: GetServerSideProps = async ({query, res}) => {
'svg': 'image/svg+xml',
'svgz': 'image/svg+xml',
'txt': 'text/plain',
'xls' : 'application/vnd.ms-excel'
};

const name = decoded.path.substring(decoded.path.lastIndexOf('/') + 1)
Expand Down
8 changes: 4 additions & 4 deletions formulaire/pages/dossier/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { StateProvider } from 'src/context/StateContext';
import DossierForm from '../../src/components/Dossier/DossierForm';
import HeadingDossier from '../../src/components/Dossier/HeadingDossier';
import Layout from '../../src/components/Layout'
import { DossierData, getDossier } from '../../src/fetching/dossiers';
import { DossierData, getDossier, ResDossier } from '../../src/fetching/dossiers';

const DossierPage: React.FC = () => {
const router = useRouter();
const [dossier, setDossier] = React.useState<DossierData>()
const [dossier, setDossier] = React.useState<ResDossier>()

const fetchDossier = async () => {
if (router.query.id) {
Expand All @@ -28,15 +28,15 @@ const DossierPage: React.FC = () => {

{dossier &&
<>
<HeadingDossier dossier={dossier}></HeadingDossier>
<HeadingDossier dossier={dossier.dossier}></HeadingDossier>
</>
}
<Container>
<Link href='/'>Retour aux dossiers</Link>
{dossier &&
<>
<StateProvider>
<DossierForm dossier={dossier}></DossierForm>
<DossierForm dossier={dossier.dossier} docs={dossier.docs}></DossierForm>
</StateProvider>
</>
}
Expand Down
16 changes: 8 additions & 8 deletions formulaire/src/components/Dossier/DossierForm.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { Demandeur, Enfant, SocieteProduction } from "@prisma/client";
import React, { useContext } from "react";
import { DossierData, EnfantData, getDossier, updateDossier } from "../../fetching/dossiers";
import { DossierData, EnfantData, getDossier, ResDocs, updateDossier } from "../../fetching/dossiers";
import { ButtonLink } from "../../uiComponents/button";
import styles from "./DossierForm.module.scss";
import "react-datepicker/dist/react-datepicker.css";
Expand All @@ -15,25 +15,20 @@ import Image from "next/image";
import { sendDossier } from "src/fetching/sync";
import IconLoader from "../IconLoader";
import useStateContext from "src/context/StateContext";
import { updateSociete } from "src/fetching/societeProduction";

interface Props {
dossier: DossierData
docs: ResDocs
}

const DossierForm: React.FC<Props> = ({ dossier }) => {
const DossierForm: React.FC<Props> = ({ dossier, docs }) => {
const router = useRouter()

const [toDisplay, setTodisplay] = React.useState<'Demandeur' | 'Projet' | 'Enfants'>('Demandeur')
const [enfantsTmp, setEnfants] = React.useState<EnfantData[]>(dossier.enfants)
const [messageError, setMessageError] = React.useState<string>('')
const [messageSuccess, setMessageSuccess] = React.useState<string>('')
const contextDossier = {...useStateContext()}

const receiveDataEnfants = (received: EnfantData[]) => {
setEnfants(received)
}

const saveDossier = useDebouncedCallback(() => {
console.log('saving dossier ... : ', contextDossier.dossier)
updateDossier(contextDossier.dossier)
Expand Down Expand Up @@ -148,8 +143,13 @@ const DossierForm: React.FC<Props> = ({ dossier }) => {
contextDossier.processEntity('demandeur', dossier.Demandeur)
contextDossier.processEntity('societeProduction', dossier.Demandeur.societeProduction ?? {})
contextDossier.processEntity('enfants', dossier.enfants)
contextDossier.processEntity('docs', docs)
}, [])

React.useEffect(() => {
console.log('docs : ', contextDossier.docs)
}, [contextDossier.docs])

return (
<div className={styles.dossierForm}>

Expand Down
8 changes: 8 additions & 0 deletions formulaire/src/components/Dossier/EnfantForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { createPieceEnfant, deletePieceEnfant } from "src/fetching/pieceEnfant";
import InputAutocomplete from "../uiComponents/InputAutocomplete";
import { uploadDoc } from "src/fetching/docs";
import Link from "next/link";
import useStateContext from "src/context/StateContext";

interface Props {
enfant: EnfantData;
Expand All @@ -26,6 +27,7 @@ const EnfantForm: React.FC<Props> = ({enfant, allowChanges, refresh}) => {
const [dataPassed, setDataPassed] = React.useState<Record<'nom' | 'prenom', string>>()
const [initialRender, setInitialRender] = React.useState<Boolean>(true)
const [initialDataPassed, setInitialDataPassed] = React.useState<Boolean>(true)
const contextDossier = {...useStateContext()}

const handleFormEnfant = (e: React.FormEvent<HTMLInputElement>): void => {
setEnfant({
Expand Down Expand Up @@ -374,6 +376,7 @@ const EnfantForm: React.FC<Props> = ({enfant, allowChanges, refresh}) => {

<InputFile id={'LIVRET_FAMILLE'}
docs={enfantTmp.piecesDossier || []}
docsTokenized={contextDossier.docs.enfants.find(enfant => enfant.id === enfantTmp.id)}
allowChanges={!allowChanges}
label={`Livret de Famille *`}
handleFile={handleFile}
Expand All @@ -387,6 +390,7 @@ const EnfantForm: React.FC<Props> = ({enfant, allowChanges, refresh}) => {

<InputFile id={'AUTORISATION_PARENTALE'}
docs={enfantTmp.piecesDossier || []}
docsTokenized={contextDossier.docs.enfants.find(enfant => enfant.id === enfantTmp.id)}
allowChanges={!allowChanges}
label={`Autorisation parentale *`}
handleFile={handleFile}
Expand All @@ -400,6 +404,7 @@ const EnfantForm: React.FC<Props> = ({enfant, allowChanges, refresh}) => {

<InputFile id={'SITUATION_PARTICULIERE'}
docs={enfantTmp.piecesDossier || []}
docsTokenized={contextDossier.docs.enfants.find(enfant => enfant.id === enfantTmp.id)}
allowChanges={!allowChanges}
label={`Situation particulière relative à l'autorité parentale`}
handleFile={handleFile}
Expand All @@ -413,6 +418,7 @@ const EnfantForm: React.FC<Props> = ({enfant, allowChanges, refresh}) => {

<InputFile id={'CONTRAT'}
docs={enfantTmp.piecesDossier || []}
docsTokenized={contextDossier.docs.enfants.find(enfant => enfant.id === enfantTmp.id)}
allowChanges={!allowChanges}
label={`Projet de contrat de travail *`}
handleFile={handleFile}
Expand All @@ -426,6 +432,7 @@ const EnfantForm: React.FC<Props> = ({enfant, allowChanges, refresh}) => {

<InputFile id={'CERTIFICAT_SCOLARITE'}
docs={enfantTmp.piecesDossier || []}
docsTokenized={contextDossier.docs.enfants.find(enfant => enfant.id === enfantTmp.id)}
allowChanges={!allowChanges}
label={`Certificat de scolarité ou avis pédagogique`}
handleFile={handleFile}
Expand All @@ -440,6 +447,7 @@ const EnfantForm: React.FC<Props> = ({enfant, allowChanges, refresh}) => {

<InputFile id={'AVIS_MEDICAL'}
docs={enfantTmp.piecesDossier || []}
docsTokenized={contextDossier.docs.enfants.find(enfant => enfant.id === enfantTmp.id)}
allowChanges={!allowChanges}
label={`Avis médical d'aptitude`}
handleFile={handleFile}
Expand Down
5 changes: 5 additions & 0 deletions formulaire/src/components/Dossier/ProjectForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ const ProjectForm: React.FC<Props> = ({ allowChanges }) => {
<div className={styles.blocForm}>
<InputFile id={'SYNOPSIS'}
docs={contextDossier.dossier.piecesDossier}
docsTokenized={contextDossier.docs.dossier}
label={'Synopsis'}
allowChanges={!allowChanges}
handleFile={handleFile}
Expand All @@ -126,6 +127,7 @@ const ProjectForm: React.FC<Props> = ({ allowChanges }) => {
<div className={styles.blocForm}>
<InputFile id={'SCENARIO'}
docs={contextDossier.dossier.piecesDossier}
docsTokenized={contextDossier.docs.dossier}
label={'Scenario ou script *'}
allowChanges={!allowChanges}
handleFile={handleFile}
Expand All @@ -137,6 +139,7 @@ const ProjectForm: React.FC<Props> = ({ allowChanges }) => {
<div className={styles.blocForm}>
<InputFile id={'MESURES_SECURITE'}
docs={contextDossier.dossier.piecesDossier}
docsTokenized={contextDossier.docs.dossier}
label={'Note précisant les mesures de sécurité *'}
allowChanges={!allowChanges}
handleFile={handleFile}
Expand All @@ -148,6 +151,7 @@ const ProjectForm: React.FC<Props> = ({ allowChanges }) => {
<div className={styles.blocForm}>
<InputFile id={'PLAN_TRAVAIL'}
docs={contextDossier.dossier.piecesDossier}
docsTokenized={contextDossier.docs.dossier}
label={'Plan de travail'}
allowChanges={!allowChanges}
handleFile={handleFile}
Expand Down Expand Up @@ -204,6 +208,7 @@ const ProjectForm: React.FC<Props> = ({ allowChanges }) => {

<InputFile id={'INFOS_COMPLEMENTAIRES'}
docs={contextDossier.dossier.piecesDossier}
docsTokenized={contextDossier.docs.dossier}
label={`Éléments d'information complémentaires`}
allowChanges={!allowChanges}
handleFile={handleFile}
Expand Down
19 changes: 15 additions & 4 deletions formulaire/src/components/uiComponents/InputFile.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JustificatifDossier, JustificatifEnfant, STATUT_PIECE } from "@prisma/client";
import { JustificatifDossier, JustificatifEnfant, PieceDossier, STATUT_PIECE } from "@prisma/client";
import Image from "next/image";
import React from "react";
import CountPieces from "../CountPieces";
Expand All @@ -16,12 +16,13 @@ interface Props {
label: string,
text: string,
docs: Doc[],
docsTokenized?: {id: number, piecesDossier: (PieceDossier & {path: string})[]}
allowChanges: Boolean,
handleFile: (e: React.ChangeEvent<HTMLInputElement>) => void,
handleDelete: (id: string) => void
}

const InputFile: React.FC<Props> = ({ id, label, text, docs, allowChanges, handleFile, handleDelete }) => {
const InputFile: React.FC<Props> = ({ id, label, text, docs, docsTokenized, allowChanges, handleFile, handleDelete }) => {

return (
<div className={styles.InputFile}>
Expand Down Expand Up @@ -50,7 +51,17 @@ const InputFile: React.FC<Props> = ({ id, label, text, docs, allowChanges, handl
/>
</div>
}
<span key={`piece_justificative_${id}_${index}`} className={`${doc.statut === 'REFUSE' ? styles.refused : doc.statut === 'VALIDE' ? styles.accepted : ''}`}>{doc.nom}</span>

{docsTokenized &&
<a href={`${docsTokenized.piecesDossier.find(docTmp => docTmp.id === doc.id)?.link}`}
target="_blank"
key={`piece_justificative_${id}_${index}`}
className={`${doc.statut === 'REFUSE' ? styles.refused : doc.statut === 'VALIDE' ? styles.accepted : ''}`}
>{doc.nom}</a>
}
{!docsTokenized &&
<span key={`piece_justificative_${id}_${index}`} className={`${doc.statut === 'REFUSE' ? styles.refused : doc.statut === 'VALIDE' ? styles.accepted : ''}`}>{doc.nom}</span>
}
</div>
</div>
))}
Expand All @@ -61,7 +72,7 @@ const InputFile: React.FC<Props> = ({ id, label, text, docs, allowChanges, handl
<input type="file"
id={id} name="justificatif"
placeholder="Parcourir..."
accept="image/jpeg,image/gif,image/png,application/pdf,text/plain" onChange={handleFile}>
accept="image/jpeg,image/gif,image/png,application/pdf,text/plain, application/msword, application/vnd.ms-excel" onChange={handleFile}>
</input>
<p className={styles.smallText}>Documents acceptés : jpeg, png, pdf</p>
</div>
Expand Down
5 changes: 3 additions & 2 deletions formulaire/src/context/StateReducer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Demandeur, Dossier, Enfant, SocieteProduction } from "@prisma/client"
import { DossierData, EnfantData } from "src/fetching/dossiers"
import { DossierData, EnfantData, ResDocs } from "src/fetching/dossiers"
import { DemandeurData } from "src/lib/types"

export type action = 'SET_ENTITY' | 'SET_INPUT_FOR_ENTITY' | 'SET_FILE'
Expand All @@ -8,7 +8,8 @@ export const initialState = {
dossier: {} as DossierData,
demandeur: {} as DemandeurData,
enfants: [] as EnfantData[],
societeProduction: {} as SocieteProduction
societeProduction: {} as SocieteProduction,
docs: {} as ResDocs
}

export type Contextype = typeof initialState
Expand Down
Loading

0 comments on commit 6029ef5

Please sign in to comment.