Skip to content

Commit

Permalink
refactor(user): merge MeUser & User models
Browse files Browse the repository at this point in the history
  • Loading branch information
fgravin committed Nov 28, 2023
1 parent 0ae61d8 commit 4791add
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 33 deletions.
27 changes: 23 additions & 4 deletions libs/api/repository/src/lib/gn4/platform/gn4-platform.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { MeResponseApiModel } from '@geonetwork-ui/data-access/gn4'
import { MeUserModel } from '@geonetwork-ui/common/domain/model/user/user.model'
import {
MeResponseApiModel,
UserApiModel,
} from '@geonetwork-ui/data-access/gn4'
import { UserModel } from '@geonetwork-ui/common/domain/model/user/user.model'
import { Injectable } from '@angular/core'
import { AvatarServiceInterface } from '../auth/avatar.service.interface'

@Injectable()
export class Gn4PlatformMapper {
constructor(private avatarService: AvatarServiceInterface) {}
userFromApi(apiUser: MeResponseApiModel): MeUserModel {
userFromMeApi(apiUser: MeResponseApiModel): UserModel {
if (!apiUser) return null
const {
hash,
Expand All @@ -18,6 +21,22 @@ export class Gn4PlatformMapper {
...user
} = apiUser
const icon = this.avatarService.getProfileIcon(hash)
return { ...user, profileIcon: icon } as MeUserModel
return { ...user, profileIcon: icon } as UserModel
}
userFromApi(apiUser: UserApiModel): UserModel {
if (!apiUser) return null
const {
enabled,
emailAddresses,
organisation,
kind,
lastLoginDate,
accountNonExpired,
accountNonLocked,
id,
credentialsNonExpired,
...user
} = apiUser
return { ...apiUser, id: id + '' } as UserModel
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ describe('Gn4PlatformService', () => {
{
username: 'ken',
email: '[email protected]',
id: 1,
id: '1',
},
{
username: 'ryu',
email: '[email protected]',
id: 2,
id: '2',
},
])
})
Expand Down
16 changes: 8 additions & 8 deletions libs/api/repository/src/lib/gn4/platform/gn4-platform.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@ import {
UsersApiService,
} from '@geonetwork-ui/data-access/gn4'
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface'
import {
MeUserModel,
UserModel,
} from '@geonetwork-ui/common/domain/model/user/user.model'
import { UserModel } from '@geonetwork-ui/common/domain/model/user/user.model'
import { Organization } from '@geonetwork-ui/common/domain/model/record'
import { Gn4PlatformMapper } from './gn4-platform.mapper'

const minApiVersion = '4.2.0'
@Injectable()
export class Gn4PlatformService implements PlatformServiceInterface {
private readonly type = 'GeoNetwork'
private me$: Observable<MeUserModel>
private me$: Observable<UserModel>
private users$: Observable<UserModel[]>
isAnonymous$: Observable<boolean>

Expand Down Expand Up @@ -48,11 +45,14 @@ export class Gn4PlatformService implements PlatformServiceInterface {
private mapper: Gn4PlatformMapper
) {
this.me$ = this.meApi.getMe().pipe(
map((apiUser) => this.mapper.userFromApi(apiUser)),
map((apiUser) => this.mapper.userFromMeApi(apiUser)),
shareReplay({ bufferSize: 1, refCount: true })
)
this.isAnonymous$ = this.me$.pipe(map((user) => !user || !('id' in user)))
this.users$ = this.usersApi.getUsers().pipe(shareReplay())
this.users$ = this.usersApi.getUsers().pipe(
map((users) => users.map((user) => this.mapper.userFromApi(user))),
shareReplay()
)
}

getTye(): string {
Expand All @@ -66,7 +66,7 @@ export class Gn4PlatformService implements PlatformServiceInterface {
return this.isApiCompatible$
}

getMe(): Observable<MeUserModel> {
getMe(): Observable<UserModel> {
return this.me$
}

Expand Down
10 changes: 2 additions & 8 deletions libs/common/domain/src/lib/model/user/user.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface MeUserModel {
export interface UserModel {
id: string
profile: string
username: string
Expand All @@ -9,19 +9,13 @@ export interface MeUserModel {
profileIcon?: string
}

export interface UserModel {
profile?: string
surname?: string
export interface UserModela {
enabled?: boolean
username?: string
id?: number
email?: string
emailAddresses?: Set<string>
organisation?: string
kind?: string
lastLoginDate?: string
accountNonExpired?: boolean
accountNonLocked?: boolean
credentialsNonExpired?: boolean
name?: string
}
4 changes: 2 additions & 2 deletions libs/common/domain/src/lib/platform.service.interface.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Observable } from 'rxjs'
import { MeUserModel, UserModel } from './model/user/user.model'
import { UserModel } from './model/user/user.model'
import { Organization } from './model/record/organization.model'

export abstract class PlatformServiceInterface {
abstract getTye(): string
abstract getApiVersion(): Observable<string>
abstract isApiCompatible(): Observable<boolean>

abstract getMe(): Observable<MeUserModel>
abstract getMe(): Observable<UserModel>
abstract isAnonymous(): Observable<boolean>
abstract getUsers(): Observable<UserModel[]>
abstract getUsersByOrganization(
Expand Down
8 changes: 4 additions & 4 deletions libs/common/fixtures/src/lib/user.fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MeUserModel } from '@geonetwork-ui/common/domain/model/user/user.model'
import { UserModel } from '@geonetwork-ui/common/domain/model/user/user.model'

export const USER_FIXTURE = (): MeUserModel => ({
export const USER_FIXTURE = (): UserModel => ({
id: '46798',
profile: 'Administrator',
username: 'Gravin',
Expand All @@ -12,7 +12,7 @@ export const USER_FIXTURE = (): MeUserModel => ({
'https://www.gravatar.com/avatar/dbdffd183622800bcf8587328daf43a6?d=mp',
})

export const USER_FIXTURE_ANON = (): MeUserModel => ({
export const USER_FIXTURE_ANON = (): UserModel => ({
id: '161',
profile: 'Administrator',
username: 'ghost16',
Expand All @@ -24,7 +24,7 @@ export const USER_FIXTURE_ANON = (): MeUserModel => ({
'https://www.gravatar.com/avatar/dbdffd183622800bcf8587328daf43a6?d=mp',
})

export const USERS_FIXTURE = (): MeUserModel[] => [
export const USERS_FIXTURE = (): UserModel[] => [
USER_FIXTURE(),
USER_FIXTURE_ANON(),
{
Expand Down
6 changes: 3 additions & 3 deletions libs/feature/catalog/src/lib/my-org/my-org.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AvatarServiceInterface } from '@geonetwork-ui/api/repository/gn4'
import { OrganizationsServiceInterface } from '@geonetwork-ui/common/domain/organizations.service.interface'
import { BehaviorSubject, of } from 'rxjs'
import { UserApiModel } from '@geonetwork-ui/data-access/gn4'
import { MeUserModel } from '@geonetwork-ui/common/domain/model/user/user.model'
import { UserModel } from '@geonetwork-ui/common/domain/model/user/user.model'
import { HttpClientTestingModule } from '@angular/common/http/testing'
import { TranslateService } from '@ngx-translate/core'
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface'
Expand All @@ -27,7 +27,7 @@ class orgServiceMock {
organisations$ = orgs$
}

const userSubject = new BehaviorSubject<MeUserModel | null>(null)
const userSubject = new BehaviorSubject<UserModel | null>(null)
const allUsersSubject = new BehaviorSubject<UserApiModel[]>([])

class PlatformServiceMock {
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('MyOrgService', () => {
})

it('should update myOrgDataSubject when authService user$ emits a user', () => {
const user: MeUserModel = {
const user: UserModel = {
organisation: 'Géo2France',
id: '2',
profile: 'profile',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ChangeDetectionStrategy, Component, Input } from '@angular/core'
import { MeUserModel } from '@geonetwork-ui/common/domain/model/user/user.model'
import { UserModel } from '@geonetwork-ui/common/domain/model/user/user.model'

@Component({
selector: 'gn-ui-user-preview',
templateUrl: './user-preview.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class UserPreviewComponent {
@Input() user: MeUserModel
@Input() user: UserModel
@Input() avatarPlaceholder?: string

get userFullName() {
Expand Down

0 comments on commit 4791add

Please sign in to comment.