|
1 | 1 | import 'reflect-metadata'; |
2 | 2 | import { injectable } from 'tsyringe'; |
3 | 3 |
|
| 4 | +import { dummyInstitution } from '../../datasources/mockups/AdminDataSource'; |
4 | 5 | import { dummyUser } from '../../datasources/mockups/UserDataSource'; |
| 6 | +import { Institution } from '../../models/Institution'; |
5 | 7 | import { Role } from '../../models/Role'; |
6 | 8 | import { AuthJwtPayload, User } from '../../models/User'; |
7 | 9 | import { UserAuthorization } from '../UserAuthorization'; |
8 | 10 |
|
9 | 11 | @injectable() |
10 | 12 | export class UserAuthorizationMock extends UserAuthorization { |
| 13 | + // Mock institution data for testing |
| 14 | + private mockInstitutions: Institution[] = [ |
| 15 | + dummyInstitution, |
| 16 | + new Institution( |
| 17 | + 3, |
| 18 | + 'Dummy Research Institute', |
| 19 | + 3, |
| 20 | + 'https://ror.org/dummy001' |
| 21 | + ), |
| 22 | + new Institution(4, 'Test University', 4, 'https://ror.org/dummy002'), |
| 23 | + new Institution(5, 'Mock Academic Center', 4, 'https://ror.org/dummy003'), |
| 24 | + ]; |
| 25 | + |
| 26 | + async getOrCreateUserInstitution(userInfo: { |
| 27 | + institution_ror_id?: string; |
| 28 | + institution_name?: string; |
| 29 | + institution_country?: string; |
| 30 | + }): Promise<Institution | null> { |
| 31 | + // Return default institution if all fields are empty or unspecified |
| 32 | + if ( |
| 33 | + (!userInfo.institution_name || userInfo.institution_name.trim() === '') && |
| 34 | + (!userInfo.institution_ror_id || |
| 35 | + userInfo.institution_ror_id.trim() === '') |
| 36 | + ) { |
| 37 | + return this.mockInstitutions[0]; // Return dummyInstitution as default |
| 38 | + } |
| 39 | + |
| 40 | + // Try to find existing institution by ROR ID first (most reliable) |
| 41 | + if (userInfo.institution_ror_id) { |
| 42 | + const existingByRor = this.mockInstitutions.find( |
| 43 | + (inst) => inst.rorId === userInfo.institution_ror_id |
| 44 | + ); |
| 45 | + if (existingByRor) { |
| 46 | + return existingByRor; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Try to find existing institution by name (case-insensitive) |
| 51 | + if (userInfo.institution_name) { |
| 52 | + const existingByName = this.mockInstitutions.find( |
| 53 | + (inst) => |
| 54 | + inst.name.toLowerCase() === userInfo.institution_name?.toLowerCase() |
| 55 | + ); |
| 56 | + if (existingByName) { |
| 57 | + return existingByName; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Create new institution if not found |
| 62 | + const newId = Math.max(...this.mockInstitutions.map((i) => i.id)) + 1; |
| 63 | + |
| 64 | + const newInstitution = new Institution( |
| 65 | + newId, |
| 66 | + userInfo.institution_name ?? 'Unknown Institution', |
| 67 | + 1, |
| 68 | + userInfo.institution_ror_id |
| 69 | + ); |
| 70 | + |
| 71 | + // Add to mock data for subsequent calls |
| 72 | + this.mockInstitutions.push(newInstitution); |
| 73 | + |
| 74 | + return newInstitution; |
| 75 | + } |
11 | 76 | async externalTokenLogin( |
12 | 77 | token: string, |
13 | 78 | _redirectUri: string |
|
0 commit comments