Skip to content

Commit

Permalink
feat(me): my-org service ok
Browse files Browse the repository at this point in the history
  • Loading branch information
cmoinier committed Oct 5, 2023
1 parent c25499e commit ac7cb46
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { UiInputsModule } from '@geonetwork-ui/ui/inputs'
import { TranslateModule } from '@ngx-translate/core'
import { CommonModule } from '@angular/common'
import { MyOrgService } from '@geonetwork-ui/feature/catalog'
import { SearchFacade } from '@geonetwork-ui/feature/search'
import { UserApiModel } from '@geonetwork-ui/data-access/gn4'

@Component({
selector: 'md-editor-my-org-users',
Expand All @@ -17,54 +19,30 @@ import { MyOrgService } from '@geonetwork-ui/feature/catalog'
CommonModule,
],
})
// export class MyOrgUsersComponent {
// orgName: string
// logoUrl: string
// userList: any[]

// constructor(
// public searchFacade: SearchFacade,
// public authService: AuthService,
// private orgService: OrganizationsServiceInterface
// ) {
// this.searchFacade.resetSearch()
// this.authService.user$.subscribe((user) => {
// this.orgName = 'Géo2France'
// // this.orgName = user?.organisation
// })
// this.orgService.organisations$.subscribe((orgs) => {
// const org = orgs.filter((org) => org.name === this.orgName)[0]
// this.logoUrl = org?.logoUrl?.href.toString()
// })
// this.authService.allUsers$.subscribe((users) => {
// this.userList = users.filter(
// (user) => user.organisation === 'Barbie Inc.'
// // (user) => user.organisation === this.orgName
// )
// })
// }
// }
export class MyOrgRecordsComponent implements OnDestroy {
export class MyOrgUsersComponent implements OnDestroy {
orgData: {
orgName: string
logoUrl: string
recordCount: number
userCount: number
userList: UserApiModel[]
}

private myOrgDataSubscription

constructor(private myOrgRecordsService: MyOrgService) {
constructor(
private myOrgRecordsService: MyOrgService,
public searchFacade: SearchFacade
) {
this.searchFacade.resetSearch()
this.myOrgDataSubscription = this.myOrgRecordsService.myOrgData$.subscribe(
(data) => {
console.log(data)
this.orgData = data
}
)
}

ngOnDestroy() {
// Don't forget to unsubscribe to avoid memory leaks
this.myOrgDataSubscription.unsubscribe()
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
<md-editor-records-list
[logo]="logoUrl"
[title]="orgName"
[recordCount]="recordCount"
[userCount]="userCount"
>
</md-editor-records-list>
<md-editor-records-list [logo]="orgData.logoUrl" [title]="orgData.orgName" [recordCount]="orgData.recordCount"
[userCount]="orgData.userCount">
</md-editor-records-list>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { OrganizationsServiceInterface } from '@geonetwork-ui/common/domain/organizations.service.interface'
import { AuthService } from '@geonetwork-ui/api/repository/gn4'
import { SearchFacade } from '@geonetwork-ui/feature/search'
import { MyOrgService } from '@geonetwork-ui/feature/catalog'

const user = USER_FIXTURE()
const filters = FILTERS_AGGREGATION
Expand All @@ -20,6 +21,7 @@ describe('MyOrgRecordsComponent', () => {
let searchFacade: SearchFacade
let orgService: OrganizationsServiceInterface
let authService: AuthService
let myOrgService: MyOrgService

beforeEach(() => {
const authServiceMock = {
Expand All @@ -41,7 +43,7 @@ describe('MyOrgRecordsComponent', () => {
orgService = organisationsServiceMock as any
searchFacade = searchFacadeMock as any

component = new MyOrgRecordsComponent(searchFacade, authService, orgService)
component = new MyOrgRecordsComponent(myOrgService)
})

it('should create', () => {
Expand All @@ -64,15 +66,17 @@ describe('MyOrgRecordsComponent', () => {

describe('Get organization users info', () => {
it('should get the org name', () => {
expect(component.orgName).toEqual('Région Hauts-de-France') // Replace with your test data
expect(component.orgData.orgName).toEqual('Région Hauts-de-France') // Replace with your test data
})

it('should get the org logo', () => {
expect(component.logoUrl).toEqual('https://my-geonetwork.org/logo11.png') // Replace with your test data
expect(component.orgData.logoUrl).toEqual(
'https://my-geonetwork.org/logo11.png'
) // Replace with your test data
})

it('should get the list of users', () => {
expect(component.userCount).toEqual(3)
expect(component.orgData.userCount).toEqual(3)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { Component, OnDestroy } from '@angular/core'
import { CommonModule } from '@angular/common'
import { TranslateModule } from '@ngx-translate/core'
import { RecordsListComponent } from '../records-list.component'
import { MyOrgService } from '@geonetwork-ui/feature/catalog'
import { SearchFacade } from '@geonetwork-ui/feature/search'
import { OrganizationsServiceInterface } from '@geonetwork-ui/common/domain/organizations.service.interface'
import { Organization } from '@geonetwork-ui/common/domain/record'
import { Subscription } from 'rxjs'
import { AuthService } from '@geonetwork-ui/api/repository/gn4'
import { OrganizationsServiceInterface } from '@geonetwork-ui/common/domain/organizations.service.interface'
import { UserApiModel } from '@geonetwork-ui/data-access/gn4'

@Component({
selector: 'md-editor-my-org-records',
Expand All @@ -15,40 +15,39 @@ import { AuthService } from '@geonetwork-ui/api/repository/gn4'
standalone: true,
imports: [CommonModule, TranslateModule, RecordsListComponent],
})
export class MyOrgRecordsComponent {
orgName: string
logoUrl: string
recordCount: number
userCount: number
export class MyOrgRecordsComponent implements OnDestroy {
orgData: {
orgName: string
logoUrl: string
recordCount: number
userCount: number
userList: UserApiModel[]
}

private myOrgDataSubscription

constructor(
private myOrgRecordsService: MyOrgService,
public searchFacade: SearchFacade,
private authService: AuthService,
private orgService: OrganizationsServiceInterface
) {
this.searchFacade.resetSearch()
this.authService.user$.subscribe((user) => {
// this.orgName = 'Géo2France'
this.orgName = user?.organisation
this.searchByOrganisation({ name: this.orgName })
})
this.orgService.organisations$.subscribe((orgs) => {
const org = orgs.filter((org) => org.name === this.orgName)[0]
this.logoUrl = org?.logoUrl?.href.toString()
this.recordCount = org?.recordCount
})

this.authService.allUsers$.subscribe((users) => {
this.userCount = users.filter(
// (user) => user.organisation === 'Barbie Inc.'
(user) => user.organisation === this.orgName
).length
})
this.myOrgDataSubscription = this.myOrgRecordsService.myOrgData$.subscribe(
(data) => {
console.log(data)
this.orgData = data
this.searchByOrganisation({ name: data.orgName })
}
)
}

searchByOrganisation(organisation: Organization) {
this.orgService
.getFiltersForOrgs([organisation])
.subscribe((filters) => this.searchFacade.setFilters(filters))
}

ngOnDestroy() {
this.myOrgDataSubscription.unsubscribe()
}
}
26 changes: 11 additions & 15 deletions libs/feature/catalog/src/lib/my-org/my-org.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Injectable } from '@angular/core'
import { SearchFacade } from '@geonetwork-ui/feature/search'
import { OrganizationsServiceInterface } from '@geonetwork-ui/common/domain/organizations.service.interface'
import { AuthService } from '@geonetwork-ui/api/repository/gn4'
import { Organization } from '@geonetwork-ui/common/domain/record'
import { BehaviorSubject, Observable } from 'rxjs'
import { UserApiModel } from '@geonetwork-ui/data-access/gn4'

@Injectable({
providedIn: 'root',
Expand All @@ -14,22 +13,24 @@ export class MyOrgService {
logoUrl: string
recordCount: number
userCount: number
userList: UserApiModel[]
}>

private myOrgDataSubject = new BehaviorSubject<{
orgName: string
logoUrl: string
recordCount: number
userCount: number
userList: UserApiModel[]
}>({
orgName: '',
logoUrl: '',
recordCount: 0,
userCount: 0,
userList: [],
})

constructor(
private searchFacade: SearchFacade,
private authService: AuthService,
private orgService: OrganizationsServiceInterface
) {
Expand All @@ -38,12 +39,12 @@ export class MyOrgService {
this.authService.user$.subscribe((user) => {
// const orgName = user?.organisation
const orgName = 'Géo2France'
this.myOrgDataSubject.next({ orgName, ...this.myOrgDataSubject.value })
this.searchByOrganisation({ name: orgName })
this.myOrgDataSubject.next({ ...this.myOrgDataSubject.value, orgName })
})

this.orgService.organisations$.subscribe((orgs) => {
const orgName = this.myOrgDataSubject.value.orgName
console.log(orgName)
const org = orgs.find((org) => org.name === orgName)
const logoUrl = org?.logoUrl?.href.toString()
const recordCount = org?.recordCount
Expand All @@ -55,17 +56,12 @@ export class MyOrgService {
})

this.authService.allUsers$.subscribe((users) => {
const orgName = this.myOrgDataSubject.value.orgName
const userCount = users.filter(
(user) => user.organisation === orgName
).length
// const orgName = this.myOrgDataSubject.value.orgName
const orgName = 'Barbie Inc.'
const userList = users.filter((user) => user.organisation === orgName)
const userCount = userList.length
this.myOrgDataSubject.next({ ...this.myOrgDataSubject.value, userList })
this.myOrgDataSubject.next({ ...this.myOrgDataSubject.value, userCount })
})
}

private searchByOrganisation(organisation: Organization) {
this.orgService
.getFiltersForOrgs([organisation])
.subscribe((filters) => this.searchFacade.setFilters(filters))
}
}

0 comments on commit ac7cb46

Please sign in to comment.