Skip to content

Commit

Permalink
feat(procedures): dgpr removal of mocks (#13934)
Browse files Browse the repository at this point in the history
ref: MANAGER-15473

Signed-off-by: Omar ALKABOUSS MOUSSANA <[email protected]>
  • Loading branch information
oalkabouss authored and Omar ALKABOUSS MOUSSANA committed Nov 7, 2024
1 parent 05beac0 commit c56d686
Show file tree
Hide file tree
Showing 15 changed files with 167 additions and 267 deletions.
45 changes: 0 additions & 45 deletions packages/manager/apps/procedures/src/data/api/documentsApi.ts

This file was deleted.

59 changes: 59 additions & 0 deletions packages/manager/apps/procedures/src/data/api/proceduresApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { v6 } from '@ovh-ux/manager-core-api';
import axios from 'axios';
import { Procedure } from '@/types/procedure';

export type UploadLink = {
link: string;
method: string;
headers: any;
};

const s3AxiosInstance = axios.create({});

const uploadDocument: (link: UploadLink, file: File) => Promise<void> = (
link,
file,
) => {
return s3AxiosInstance.put(link.link, file, {
headers: {
...link.headers,
},
});
};

export const getProceduresAPI = (procedure: Procedure) => {
const uri = `/me/procedure/${procedure}`;

const getStatus: <TResponse>() => Promise<TResponse> = async () => {
const { data } = await v6.get(uri);
return data;
};

const getDocumentsLinks = <TData extends { numberOfDocuments: number }>(
dataQuery: TData,
): Promise<UploadLink[]> => {
return v6
.post(uri, dataQuery)
.then(({ data: dataResponse }) => dataResponse.uploadLinks);
};

const finalize: () => Promise<void> = () => {
return v6.post(`${uri}/finalize`).then(({ data }) => data);
};

const uploadDocuments: (
files: File[],
links: UploadLink[],
) => Promise<void> = async (files, links) => {
await Promise.all(
links.map((link, index) => uploadDocument(link, files[index])),
);
return finalize();
};

return {
getStatus,
getDocumentsLinks,
uploadDocuments,
};
};
71 changes: 0 additions & 71 deletions packages/manager/apps/procedures/src/data/api/rgdp/rgdpApi.ts

This file was deleted.

7 changes: 0 additions & 7 deletions packages/manager/apps/procedures/src/data/api/statusApi.ts

This file was deleted.

50 changes: 0 additions & 50 deletions packages/manager/apps/procedures/src/data/hooks/rgdp/useRGDP.tsx

This file was deleted.

51 changes: 0 additions & 51 deletions packages/manager/apps/procedures/src/data/hooks/useDocuments.tsx

This file was deleted.

66 changes: 66 additions & 0 deletions packages/manager/apps/procedures/src/data/hooks/useProcedures.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { useMutation, useQuery } from '@tanstack/react-query';
import { AxiosError } from 'axios';
import { getProceduresAPI, UploadLink } from '@/data/api/proceduresApi';
import { Procedure } from '@/types/procedure';

export type UploadDocumentsProps = {
files: File[];
links: UploadLink[];
};

export const useProcedures = (procedure: Procedure) => {
const { getStatus, getDocumentsLinks, uploadDocuments } = getProceduresAPI(
procedure,
);

const useStatus = <TResponse,>() =>
useQuery<TResponse, AxiosError>({
queryKey: ['getStatus', procedure],
queryFn: getStatus,
retry: 0,
});

const useUploadLinks = <TData extends { numberOfDocuments: number }>({
onSuccess,
onError,
}: {
onSuccess: (links: UploadLink[]) => void;
onError: () => void;
}) =>
useMutation({
mutationFn: (data: TData) => getDocumentsLinks(data),
onSuccess: (links) => {
onSuccess?.(links);
},
onError: () => {
onError?.();
},
});

const useUploadDocuments = ({
onSuccess,
onError,
}: {
onSuccess: () => void;
onError: () => void;
}) =>
useMutation({
mutationFn: ({ files, links }: UploadDocumentsProps) => {
return uploadDocuments(files, links);
},
onSuccess: () => {
onSuccess?.();
},
onError: () => {
// TODO: add logs to better understand what is going wrong on procedure finalization (MANAGER-15281)
onError?.();
},
retry: 1,
retryDelay: 3000,
});
return {
useStatus,
useUploadLinks,
useUploadDocuments,
};
};
11 changes: 0 additions & 11 deletions packages/manager/apps/procedures/src/data/hooks/useStatus.tsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { describe, expect, it, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import Home from './DisableMFA.page';
import { useProcedures } from '@/data/hooks/useProcedures';

const mockedUsedNavigate = vi.fn();
const mockedUsedLocation = vi.fn();
Expand All @@ -28,8 +29,10 @@ vi.mock('@/components/Loading/Loading', () => ({
default: () => <p>TestLoading</p>,
}));

vi.mock('@/data/hooks/useStatus', () => ({
useFetch2faStatus: () => fetch2faStatusFakeResponse,
vi.mock('@/data/hooks/useProcedures', () => ({
useProcedures: () => ({
useStatus: () => fetch2faStatusFakeResponse,
}),
}));

describe('DisableMFA.page', () => {
Expand Down
Loading

0 comments on commit c56d686

Please sign in to comment.