(backend) manage reconciliation requests for user accounts (#1878)

For now, the reconciliation requests are imported through CSV in the
Django admin, which sends confirmation email to both addresses. When
both are checked, the actual reconciliation is processed, and all
user-related content is updated.

## Purpose

Fix #1616 // Replaces #1708

For now, the reconciliation requests are imported through CSV in the
Django admin, which sends confirmation email to both addresses. When
both are checked, the actual reconciliation is processed, and all
user-related content is updated.


## Proposal
- [x] New `UserReconciliationCsvImport` model to manage the import of
reconciliation requests through a task
(`user_reconciliation_csv_import_job`)
- [x] New `UserReconciliation` model to store the user reconciliation
requests themselves (a row = a `active_user`/`inactive_user` pair)
  - [x] On save, a confirmation email is sent to the users
- [x] A `process_reconciliation` admin action process the action on the
requested entries, if both emails have been checked.
- [x] Bulk update the `DocumentAccess` items, while managing the case
where both users have access to the document (keeping the higher role)
- [x] Bulk update the `LinkTrace` items, while managing the case where
both users have link traces to the document
- [x] Bulk update the `DocumentFavorite` items, while managing the case
where both users have put the document in their favorites
- [x] Bulk update the comment system items (`Thread`, `Comment` and
`Reaction` items)
  - [x] Bulk update the `is_active` status on both users
- [x] New `USER_RECONCILIATION_FORM_URL` env variable for the "make a
new request" URL in an email.
- [x] Write unit tests
- [x] Remove the unused `email_user()` method on `User`, replaced with
`send_email()` similar to the one on the `Document` model


## Demo page reconciliation success

<img width="1149" height="746" alt="image"
src="https://github.com/user-attachments/assets/09ba2b38-7af3-41fa-a64f-ce3c4fd8548d"
/>

---------

Co-authored-by: Anthony LC <anthony.le-courric@mail.numerique.gouv.fr>
This commit is contained in:
Sylvain Boissel
2026-02-11 19:09:20 +01:00
committed by GitHub
parent 685464f2d7
commit 3ab0a47c3a
33 changed files with 2015 additions and 39 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

View File

@@ -0,0 +1,68 @@
import { render, screen, waitFor } from '@testing-library/react';
import fetchMock from 'fetch-mock';
import React from 'react';
import { describe, expect, test, vi } from 'vitest';
import { AppWrapper } from '@/tests/utils';
import { UserReconciliation } from '../components/UserReconciliation';
vi.mock('../assets/mail-check-filled.svg', () => ({
default: () => <div data-testid="success-svg">SuccessSvg</div>,
}));
describe('UserReconciliation', () => {
beforeEach(() => {
fetchMock.reset();
});
['active', 'inactive'].forEach((type) => {
test(`renders when reconciliation is a ${type} success`, async () => {
fetchMock.get(
`http://test.jest/api/v1.0/user-reconciliations/${type}/123456/`,
{ details: 'Success' },
);
render(
<UserReconciliation
type={type as 'active' | 'inactive'}
reconciliationId="123456"
/>,
{
wrapper: AppWrapper,
},
);
await waitFor(() => {
expect(fetchMock.calls().length).toBe(1);
});
expect(
await screen.findByText(
/To complete the unification of your user accounts/i,
),
).toBeInTheDocument();
});
});
test('renders when reconciliation fails', async () => {
fetchMock.get(
`http://test.jest/api/v1.0/user-reconciliations/active/invalid-id/`,
{
throws: new Error('invalid id'),
},
);
render(<UserReconciliation type="active" reconciliationId="invalid-id" />, {
wrapper: AppWrapper,
});
await waitFor(() => {
expect(fetchMock.calls().length).toBe(1);
});
expect(
await screen.findByText(/An error occurred during email validation./i),
).toBeInTheDocument();
});
});

View File

@@ -1,2 +1,3 @@
export * from './useAuthQuery';
export * from './types';
export * from './useAuthQuery';
export * from './useUserReconciliations';

View File

@@ -0,0 +1,51 @@
import { UseQueryOptions, useQuery } from '@tanstack/react-query';
import { APIError, errorCauses, fetchAPI } from '@/api';
type UserReconciliationResponse = {
details: string;
};
interface UserReconciliationProps {
type: 'active' | 'inactive';
reconciliationId?: string;
}
export const userReconciliations = async ({
type,
reconciliationId,
}: UserReconciliationProps): Promise<UserReconciliationResponse> => {
const response = await fetchAPI(
`user-reconciliations/${type}/${reconciliationId}/`,
);
if (!response.ok) {
throw new APIError(
'Failed to do the user reconciliation',
await errorCauses(response),
);
}
return response.json() as Promise<UserReconciliationResponse>;
};
export const KEY_USER_RECONCILIATIONS = 'user_reconciliations';
export function useUserReconciliationsQuery(
param: UserReconciliationProps,
queryConfig?: UseQueryOptions<
UserReconciliationResponse,
APIError,
UserReconciliationResponse
>,
) {
return useQuery<
UserReconciliationResponse,
APIError,
UserReconciliationResponse
>({
queryKey: [KEY_USER_RECONCILIATIONS, param],
queryFn: () => userReconciliations(param),
...queryConfig,
});
}

View File

@@ -0,0 +1,14 @@
<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_12770_18024)">
<path fill-rule="evenodd" clip-rule="evenodd" d="M26.5924 17.5492C27.3201 17.5492 28.0035 17.686 28.6419 17.9606C29.2802 18.2352 29.8434 18.6165 30.3307 19.1038C30.8179 19.5911 31.1994 20.1579 31.474 20.8031C31.7551 21.4411 31.8958 22.1241 31.8958 22.8512C31.8958 23.5717 31.7552 24.2521 31.474 24.8903C31.1994 25.5354 30.8144 26.1022 30.3203 26.5895C29.833 27.0768 29.2663 27.4582 28.6211 27.7327C27.9828 28.0141 27.3063 28.1546 26.5924 28.1546C25.8649 28.1546 25.1813 28.0177 24.543 27.7432C23.9049 27.4687 23.3427 27.0835 22.8555 26.5895C22.3682 26.1022 21.9833 25.539 21.7018 24.9007C21.4273 24.2624 21.2904 23.5787 21.2904 22.8512C21.2904 22.124 21.4274 21.4412 21.7018 20.8031C21.9832 20.1647 22.3682 19.6016 22.8555 19.1143C23.3427 18.6202 23.9049 18.2351 24.543 17.9606C25.1813 17.6861 25.8649 17.5492 26.5924 17.5492ZM28.9609 20.1846C28.7003 20.1846 28.4947 20.2947 28.3438 20.514L25.9128 23.8708L24.7188 22.5635C24.6502 22.4949 24.5709 22.4406 24.4818 22.3994C24.3926 22.3582 24.2863 22.3369 24.1628 22.3369C23.9637 22.3369 23.7913 22.4054 23.6471 22.5426C23.5032 22.673 23.431 22.8492 23.431 23.0687C23.4311 23.1576 23.4489 23.2467 23.4831 23.3356C23.5242 23.4316 23.5763 23.5215 23.638 23.6038L25.3672 25.4775C25.4358 25.5598 25.5257 25.6213 25.6354 25.6624C25.7451 25.7036 25.8515 25.7249 25.9544 25.7249C26.2219 25.7249 26.4239 25.6314 26.5612 25.4463L29.5378 21.3486C29.5926 21.2732 29.6304 21.1975 29.651 21.1221C29.6784 21.0468 29.6913 20.978 29.6914 20.9163C29.6914 20.7105 29.6158 20.538 29.4648 20.4007C29.3208 20.2569 29.1529 20.1846 28.9609 20.1846Z" fill="#367664"/>
<path d="M20.293 19.5153C20.0526 19.9618 19.8626 20.4428 19.7253 20.958C19.5948 21.4663 19.5299 21.9958 19.5299 22.5452C19.53 22.9022 19.5606 23.2528 19.6224 23.596C19.6842 23.9393 19.7704 24.2756 19.8802 24.6051H4.1888C3.81102 24.6051 3.47375 24.5639 3.17839 24.4814C2.88337 24.4059 2.63299 24.2996 2.42708 24.1624L10.2878 16.29L11.6992 17.5479C12.0221 17.8295 12.3451 18.0394 12.668 18.1768C12.9906 18.314 13.3271 18.3824 13.6771 18.3825C14.0273 18.3825 14.3647 18.3141 14.6875 18.1768C15.0172 18.0394 15.3438 17.8295 15.6667 17.5479L17.0781 16.29L20.293 19.5153Z" fill="#367664"/>
<path d="M8.97917 15.1468L1.32422 22.7822C1.24179 22.583 1.17699 22.3589 1.12891 22.1117C1.08772 21.8644 1.06641 21.5789 1.06641 21.2562V9.86165C1.06641 9.51134 1.08994 9.20825 1.13802 8.9541C1.19294 8.69333 1.25202 8.49729 1.3138 8.36686L8.97917 15.1468Z" fill="#367664"/>
<path d="M26.0521 8.36686C26.107 8.49725 26.1612 8.69345 26.2161 8.9541C26.2711 9.20825 26.2995 9.51134 26.2995 9.86165V15.7757C25.3104 15.7757 24.3789 15.9821 23.5065 16.3942C22.6413 16.7994 21.8993 17.3455 21.2812 18.0322L18.3867 15.1468L26.0521 8.36686Z" fill="#367664"/>
<path d="M22.9089 6.5127C23.7331 6.5127 24.4135 6.68136 24.9492 7.0179L14.625 16.1468C14.3161 16.4281 13.9997 16.5687 13.6771 16.5687C13.3613 16.5685 13.0449 16.4283 12.7292 16.1468L2.40625 7.0179C2.94872 6.68149 3.62829 6.51278 4.44531 6.5127H22.9089Z" fill="#367664"/>
</g>
<defs>
<clipPath id="clip0_12770_18024">
<rect width="32" height="32" fill="white"/>
</clipPath>
</defs>
</svg>

After

Width:  |  Height:  |  Size: 3.2 KiB

View File

@@ -0,0 +1,97 @@
import Image from 'next/image';
import { useTranslation } from 'react-i18next';
import error_img from '@/assets/icons/error-coffee.png';
import { Box, Loading, Text } from '@/components';
import { useUserReconciliationsQuery } from '../api';
import SuccessSvg from '../assets/mail-check-filled.svg';
interface UserReconciliationProps {
reconciliationId: string;
type: 'active' | 'inactive';
}
export const UserReconciliation = ({
reconciliationId,
type,
}: UserReconciliationProps) => {
const { t } = useTranslation();
const { data: userReconciliations, isError } = useUserReconciliationsQuery({
type,
reconciliationId,
});
if (!userReconciliations && !isError) {
return (
<Loading
$height="100vh"
$width="100vw"
$position="absolute"
$css="top: 0;"
/>
);
}
let render = (
<Box $gap="xs" $align="center">
<SuccessSvg />
<Text
as="h3"
$textAlign="center"
$maxWidth="350px"
$theme="neutral"
$margin="0"
$size="16px"
>
{t('Email Address Confirmed')}
</Text>
<Text
as="p"
$textAlign="center"
$maxWidth="330px"
$theme="neutral"
$variation="secondary"
$margin="0"
$size="sm"
>
{t(
'To complete the unification of your user accounts, please click the confirmation links sent to all the email addresses you provided.',
)}
</Text>
</Box>
);
if (isError) {
render = (
<Box $gap="xs" $align="center">
<Image
src={error_img}
alt=""
width={300}
style={{
maxWidth: '100%',
height: 'auto',
}}
/>
<Text
as="p"
$textAlign="center"
$maxWidth="330px"
$theme="neutral"
$variation="secondary"
$margin="0"
$size="sm"
>
{t('An error occurred during email validation.')}
</Text>
</Box>
);
}
return (
<Box $align="center" $margin="auto" $padding={{ bottom: '2rem' }}>
{render}
</Box>
);
};

View File

@@ -1,3 +1,4 @@
export * from './Auth';
export * from './ButtonLogin';
export * from './UserAvatar';
export * from './UserReconciliation';

View File

@@ -0,0 +1,40 @@
import Head from 'next/head';
import { useRouter } from 'next/router';
import { ReactElement } from 'react';
import { useTranslation } from 'react-i18next';
import { UserReconciliation } from '@/features/auth/components/UserReconciliation';
import { PageLayout } from '@/layouts';
import { NextPageWithLayout } from '@/types/next';
const Page: NextPageWithLayout = () => {
const { t } = useTranslation();
const {
query: { id },
} = useRouter();
if (typeof id !== 'string') {
return null;
}
return (
<>
<Head>
<meta name="robots" content="noindex" />
<title>{`${t('User reconciliation')} - ${t('Docs')}`}</title>
<meta
property="og:title"
content={`${t('User reconciliation')} - ${t('Docs')}`}
key="title"
/>
</Head>
<UserReconciliation type="active" reconciliationId={id} />
</>
);
};
Page.getLayout = function getLayout(page: ReactElement) {
return <PageLayout withFooter={false}>{page}</PageLayout>;
};
export default Page;

View File

@@ -0,0 +1,40 @@
import Head from 'next/head';
import { useRouter } from 'next/router';
import { ReactElement } from 'react';
import { useTranslation } from 'react-i18next';
import { UserReconciliation } from '@/features/auth/components/UserReconciliation';
import { PageLayout } from '@/layouts';
import { NextPageWithLayout } from '@/types/next';
const Page: NextPageWithLayout = () => {
const { t } = useTranslation();
const {
query: { id },
} = useRouter();
if (typeof id !== 'string') {
return null;
}
return (
<>
<Head>
<meta name="robots" content="noindex" />
<title>{`${t('User reconciliation')} - ${t('Docs')}`}</title>
<meta
property="og:title"
content={`${t('User reconciliation')} - ${t('Docs')}`}
key="title"
/>
</Head>
<UserReconciliation type="inactive" reconciliationId={id} />
</>
);
};
Page.getLayout = function getLayout(page: ReactElement) {
return <PageLayout withFooter={false}>{page}</PageLayout>;
};
export default Page;

View File

@@ -1,4 +1,23 @@
import '@testing-library/jest-dom/vitest';
import * as dotenv from 'dotenv';
import React from 'react';
import { vi } from 'vitest';
dotenv.config({ path: './.env.test', quiet: true });
vi.mock('next/image', () => ({
__esModule: true,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
default: (props: any) => {
const {
src,
alt = '',
unoptimized: _unoptimized,
priority: _priority,
...rest
} = props;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const resolved = typeof src === 'string' ? src : src?.src;
return React.createElement('img', { src: resolved, alt, ...rest });
},
}));