-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathDataAccessUsersDataSource.ts
More file actions
154 lines (132 loc) · 4.05 KB
/
Copy pathDataAccessUsersDataSource.ts
File metadata and controls
154 lines (132 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { Country } from '../../models/Country';
import { Institution } from '../../models/Institution';
import { Rejection } from '../../models/Rejection';
import { BasicUserDetails, User } from '../../models/User';
import {
DataAccessUsersDataSource,
UserWithInstitution,
} from '../DataAccessUsersDataSource';
import { basicDummyUser, basicDummyUserNotOnProposal } from './UserDataSource';
// Mock full user objects for getDataAccessUsersWithInstitution
export const dummyDataAccessFullUser = new User(
100,
'Dr.',
'Jane',
'Smith',
'jane.smith',
'Jane',
'jane.smith.oidc',
'refresh-token',
10,
'oauth-issuer',
'jane.smith@dataaccess.org',
'2023-01-15 10:30:00+00',
'2023-01-15 10:30:00+00'
);
export const dummyDataAccessFullUser2 = new User(
101,
'Dr.',
'Bob',
'Johnson',
'Bob',
'bob.johnson.oidc',
'refresh-token',
'oauth-issuer',
11,
'Research Center',
'bob.johnson@research.org',
'2023-02-20 14:15:00+00',
'2023-02-20 14:15:00+00'
);
export const dummyInstitution = new Institution(10, 'Data Access Institute', 1);
export const dummyInstitution2 = new Institution(11, 'Research Center', 2);
export const dummyCountry = new Country(1, 'Denmark');
export const dummyCountry2 = new Country(2, 'United Kingdom');
export const dummyCountry3 = new Country(3, 'Belarus');
export default class MockDataAccessUsersDataSource
implements DataAccessUsersDataSource
{
private mockData: Map<number, number[]> = new Map([[1, []]]);
async findByProposalPk(proposalPk: number): Promise<BasicUserDetails[]> {
const userIds = this.mockData.get(proposalPk) || [];
// Return mock users based on stored user IDs
const users: BasicUserDetails[] = [];
if (userIds.includes(basicDummyUser.id)) {
users.push(basicDummyUser);
}
if (userIds.includes(basicDummyUserNotOnProposal.id)) {
users.push(basicDummyUserNotOnProposal);
}
return users;
}
async getDataAccessUsersWithInstitution(
proposalPk: number
): Promise<UserWithInstitution[]> {
const userIds = this.mockData.get(proposalPk) || [];
// Return mock users with institution and country based on stored user IDs
const users: UserWithInstitution[] = [];
if (userIds.includes(100)) {
users.push({
user: dummyDataAccessFullUser,
institution: dummyInstitution,
country: dummyCountry,
});
}
if (userIds.includes(101)) {
users.push({
user: dummyDataAccessFullUser2,
institution: dummyInstitution2,
country: dummyCountry2,
});
}
return users;
}
async updateDataAccessUsers(
proposalPk: number,
userIds: number[]
): Promise<BasicUserDetails[] | Rejection> {
try {
// Update mock data
this.mockData.set(proposalPk, userIds);
// Return updated list
return this.findByProposalPk(proposalPk);
} catch (error) {
return new Rejection('Failed to update data access users', {
proposalPk,
userIds,
error: error instanceof Error ? error.message : String(error),
});
}
}
async isDataAccessUserOfProposal(
id: number,
proposalPk: number
): Promise<boolean> {
const userIds = this.mockData.get(proposalPk) || [];
return Promise.resolve(userIds.includes(id));
}
async addDataAccessUser(
proposalPk: number,
userId: number
): Promise<Rejection | undefined> {
try {
const existingUserIds = this.mockData.get(proposalPk) || [];
// Idempotent insert - don't add the same user twice
if (!existingUserIds.includes(userId)) {
this.mockData.set(proposalPk, [...existingUserIds, userId]);
}
const users = await this.findByProposalPk(proposalPk);
const addedUser = users.find((user) => user.id === userId);
if (!addedUser) {
throw new Error(`No mock user found for id ${userId}`);
}
return;
} catch (error) {
return new Rejection('Failed to add data access user', {
proposalPk,
userId,
error: error instanceof Error ? error.message : String(error),
});
}
}
}