Skip to content

Commit

Permalink
archiver un village front et back
Browse files Browse the repository at this point in the history
  • Loading branch information
GaspardRivoire committed Jul 5, 2024
1 parent c875006 commit 7aec340
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 99 deletions.
54 changes: 53 additions & 1 deletion server/controllers/archive.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import AWS from 'aws-sdk';
import type { Request, Response, NextFunction } from 'express';

import { UserType } from '../entities/user';
Expand All @@ -7,7 +8,7 @@ import { Controller } from './controller';
const archiveController = new Controller('/archives');

// get file
archiveController.get({ path: '/*', userType: UserType.ADMIN }, async (req: Request, res: Response, next: NextFunction) => {
archiveController.get({ path: 'get-file/*', userType: UserType.ADMIN }, async (req: Request, res: Response, next: NextFunction) => {
const url = decodeURI(req.url);
const key = `archives${url}${url.split('/').length === 2 ? '/index.html' : url.indexOf('.') === -1 ? '.html' : ''}`;
try {
Expand All @@ -17,4 +18,55 @@ archiveController.get({ path: '/*', userType: UserType.ADMIN }, async (req: Requ
}
});

/**
* Liste les dossiers dans un préfixe S3 spécifié.
* @param prefix - Le préfixe S3 à partir duquel lister les dossiers.
* @returns Une promesse qui résout avec un tableau de noms de dossiers.
*/
async function listS3Folders(prefix: string): Promise<string[]> {
// Configurer le client S3 avec les informations d'identification et la région
const s3 = new AWS.S3({
accessKeyId: process.env.S3_ACCESS_KEY,
secretAccessKey: process.env.S3_SECRET_KEY,
region: 'eu-west-3',
});

// Paramètres pour la requête S3
const params: AWS.S3.ListObjectsV2Request = {
Bucket: process.env.S3_BUCKET_NAME,

Check failure on line 36 in server/controllers/archive.ts

View workflow job for this annotation

GitHub Actions / typescript

Type 'string | undefined' is not assignable to type 'string'.
Prefix: prefix,
Delimiter: '/', // Utiliser le délimiteur '/' pour obtenir uniquement les dossiers
};

try {
// Effectuer la requête pour lister les objets
const data = await s3.listObjectsV2(params).promise();

if (!data.CommonPrefixes) {
return [];
}

// Extraire les noms de dossiers des préfixes communs
// Nettoyer les noms de dossiers (supprimer le préfixe et le délimiteur final)
return data.CommonPrefixes.map((prefixObj) => prefixObj.Prefix)
.filter((prefix): prefix is string => prefix !== undefined)
.map((folder) => folder.slice(prefix.length, -1));
} catch (error) {
console.error('Error while listing S3 folders:', error);
throw error;
}
}

archiveController.get({ path: '', userType: UserType.OBSERVATOR }, async (_req: Request, res: Response, next: NextFunction) => {
try {
const prefix = 'archives/';
const archiveFolders = await listS3Folders(prefix);
res.sendJSON(archiveFolders);
} catch (error) {
console.error('Error while listing archived S3 folders:', error);
next(error);
return;
}
});

export { archiveController };
12 changes: 12 additions & 0 deletions src/api/archive/archive.get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import axios from 'axios';
import { useQuery } from 'react-query';

const BASE_URL = '/api/archives';

// Récupérer la liste des années déjà archivées
export const useListArchives = () => {
return useQuery(['archives'], async () => {
const { data } = await axios.get(`${BASE_URL}`);
return data;
});
};
18 changes: 0 additions & 18 deletions src/api/pelicoPresentation/pelicoPresentation.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,3 @@ export const usePelicoPresentatations = () => {
return data;
});
};

// async function getPelicoPresentation(id: number) {
// const { data } = await axios.get(`${BASE_URL}/${id}`);
// return data;
// }

// export const useGetPelicoPresentation = (id: number) => {
// return useQuery(['pelico-presentation'], getPelicoPresentation(id));
// };

// async function getPelicoPresentations() {
// const { data } = await axios.get(`${BASE_URL}`);
// return data;
// }

// export const useGetPelicoPresentations = () => {
// return useQuery(['pelico-presentation'], getPelicoPresentations);
// };
47 changes: 0 additions & 47 deletions src/components/admin/manage/settings/ArchiveModal.tsx

This file was deleted.

67 changes: 34 additions & 33 deletions src/pages/admin/newportal/manage/settings/archive/index.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
import Link from 'next/link';
import React, { useState } from 'react';
import { useSnackbar } from 'notistack';
import React, { useEffect, useState } from 'react';

import { Typography, Box, List, ListItem } from '@mui/material';

import { ArchiveModal } from 'src/components/admin/manage/settings/ArchiveModal';
import { RedButton } from 'src/components/buttons/RedButton';
import { useListArchives } from 'src/api/archive/archive.get';
import { UserContext } from 'src/contexts/userContext';
import BackArrow from 'src/svg/back-arrow.svg';
import { UserType } from 'types/user.type';

const Archive = () => {
const { user } = React.useContext(UserContext);
const hasAccess = user?.type === UserType.SUPER_ADMIN;
const archives = ['1V 2020/21', '1V 2021/22', '1V 2022/23'];
const [isModalOpen, setIsModalOpen] = useState<boolean>(false);
const [archives, setArchives] = useState<string[]>([]);
const { enqueueSnackbar } = useSnackbar();

const { data: listArchive, isError: listArchiveError, isLoading: listArchiveLoading } = useListArchives();

useEffect(() => {
if (listArchive) {
setArchives(listArchive);
}
if (listArchiveError) {
enqueueSnackbar("Une erreur s'est produite lors de la récuperation des archives existantes !", {
variant: 'error',
});
}
}, [listArchive, listArchiveError, enqueueSnackbar]);

if (!hasAccess) {
return <h1>Vous n&apos;avez pas accès à cette page, vous devez être super admin.</h1>;
}

const handleArchiveClick = () => {
setIsModalOpen(true);
};

const handleModalClose = () => {
setIsModalOpen(false);
};

const handleArchiveConfirm = () => {
setIsModalOpen(false);
};

return (
<div>
<Link href="/admin/newportal/manage/settings">
Expand All @@ -39,24 +40,24 @@ const Archive = () => {
<h1 style={{ marginLeft: '10px' }}>Archiver</h1>
</div>
</Link>
<p>Archiver 1Village va fermer l&apos;accès à tous les utilisateurs à la plateforme. Voilà les anciennes archives d&apos;1Village :</p>
<p>Voilà la liste des archives existantes. Si tu souhaites effectuer une nouvelle archive d&apos;1Village, adresse toi au pôle tech.</p>
<Box display="flex" flexDirection="column" alignItems="center">
<List>
{archives.map((archive, index) => (
<ListItem key={index}>
<Link href={`/archives/${archive}`}>
<Typography variant="body1">{archive}</Typography>
</Link>
</ListItem>
))}
</List>
<Box mt={4}>
<RedButton onClick={handleArchiveClick} variant="contained" size="large" fullWidth>
Archiver
</RedButton>
</Box>
{listArchiveLoading ? (
<div>Chargement...</div>
) : (
<List>
{archives.map((archive, index) => (
<ListItem key={index}>
<Link href={`/api/archives/${archive}`} passHref legacyBehavior>
<a target="_blank" rel="noopener noreferrer">
<Typography variant="body1">{archive}</Typography>
</a>
</Link>
</ListItem>
))}
</List>
)}
</Box>
<ArchiveModal open={isModalOpen} onClose={handleModalClose} onConfirm={handleArchiveConfirm} />
</div>
);
};
Expand Down

0 comments on commit 7aec340

Please sign in to comment.