107 lines
3.4 KiB
TypeScript
107 lines
3.4 KiB
TypeScript
import { http, HttpResponse } from 'msw';
|
|
import type { UserListItem } from '@/api/types';
|
|
import { API_BASE, mockUser } from '../auth/fixtures';
|
|
|
|
let mockUsers: UserListItem[] = [
|
|
{
|
|
...mockUser,
|
|
createdAt: '2026-01-01T00:00:00.000Z',
|
|
invitationPending: false,
|
|
inviteLink: null,
|
|
},
|
|
{
|
|
id: '22222222-2222-2222-2222-222222222222',
|
|
email: 'admin@example.com',
|
|
name: 'Admin User',
|
|
role: 'Administrator',
|
|
isActive: true,
|
|
createdAt: '2026-01-15T00:00:00.000Z',
|
|
invitationPending: false,
|
|
inviteLink: null,
|
|
},
|
|
{
|
|
id: '33333333-3333-3333-3333-333333333333',
|
|
email: 'pending@example.com',
|
|
name: 'pending@example.com',
|
|
role: 'User',
|
|
isActive: true,
|
|
createdAt: '2026-06-01T00:00:00.000Z',
|
|
invitationPending: true,
|
|
inviteLink: '/invite/complete?token=pending-mock-token',
|
|
},
|
|
];
|
|
|
|
export const resetMockUsers = () => {
|
|
mockUsers = [
|
|
{
|
|
...mockUser,
|
|
createdAt: '2026-01-01T00:00:00.000Z',
|
|
invitationPending: false,
|
|
inviteLink: null,
|
|
},
|
|
{
|
|
id: '22222222-2222-2222-2222-222222222222',
|
|
email: 'admin@example.com',
|
|
name: 'Admin User',
|
|
role: 'Administrator',
|
|
isActive: true,
|
|
createdAt: '2026-01-15T00:00:00.000Z',
|
|
invitationPending: false,
|
|
inviteLink: null,
|
|
},
|
|
{
|
|
id: '33333333-3333-3333-3333-333333333333',
|
|
email: 'pending@example.com',
|
|
name: 'pending@example.com',
|
|
role: 'User',
|
|
isActive: true,
|
|
createdAt: '2026-06-01T00:00:00.000Z',
|
|
invitationPending: true,
|
|
inviteLink: '/invite/complete?token=pending-mock-token',
|
|
},
|
|
];
|
|
};
|
|
|
|
export const getMockUsers = () => mockUsers;
|
|
|
|
export const userHandlers = [
|
|
http.get(`${API_BASE}/api/v1/Users`, () => HttpResponse.json(mockUsers)),
|
|
|
|
http.post(`${API_BASE}/api/v1/Users/invite`, async ({ request }) => {
|
|
const body = (await request.json()) as { email: string; role: string };
|
|
const token = `mock-token-${Date.now()}`;
|
|
const inviteLink = `/invite/complete?token=${token}`;
|
|
|
|
const newUser: UserListItem = {
|
|
id: crypto.randomUUID(),
|
|
email: body.email,
|
|
name: body.email,
|
|
role: body.role as UserListItem['role'],
|
|
isActive: true,
|
|
createdAt: new Date().toISOString(),
|
|
invitationPending: true,
|
|
inviteLink,
|
|
};
|
|
mockUsers = [...mockUsers, newUser];
|
|
|
|
return HttpResponse.json({ inviteLink });
|
|
}),
|
|
|
|
http.put(`${API_BASE}/api/v1/Users/:userId/role`, () => new HttpResponse(null, { status: 200 })),
|
|
|
|
http.put(`${API_BASE}/api/v1/Users/:userId/active`, () => new HttpResponse(null, { status: 200 })),
|
|
|
|
http.delete(`${API_BASE}/api/v1/Users/:userId`, ({ params }) => {
|
|
const { userId } = params as { userId: string };
|
|
const ownerUser = mockUsers.find((u) => u.role === 'Owner');
|
|
if (userId === ownerUser?.id) {
|
|
return HttpResponse.json(
|
|
{ title: 'Bad Request', detail: 'At least one Owner must remain.', status: 400 },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
mockUsers = mockUsers.filter((u) => u.id !== userId);
|
|
return new HttpResponse(null, { status: 204 });
|
|
}),
|
|
];
|