-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #981 from Vizzuality/feat/MARXAN-1466-concatenate-…
…export-and-import-operations feat: connect export and import in case is a clonning proces using ar…
- Loading branch information
Showing
8 changed files
with
119 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 76 additions & 5 deletions
81
api/apps/api/src/modules/clone/infra/export/archive-ready.saga.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,87 @@ | ||
import { | ||
ArchiveLocation, | ||
ResourceId, | ||
ResourceKind, | ||
} from '@marxan/cloning/domain'; | ||
import { UserId } from '@marxan/domain-ids'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { ICommand, ofType, Saga } from '@nestjs/cqrs'; | ||
import { Observable } from 'rxjs'; | ||
import { map } from 'rxjs/operators'; | ||
import { from, Observable } from 'rxjs'; | ||
import { mergeMap } from 'rxjs/operators'; | ||
import { ExportRepository } from '../../export/application/export-repository.port'; | ||
import { ArchiveReady } from '../../export/domain'; | ||
import { ImportProject } from '../../import/application/import-project.command'; | ||
import { ImportScenario } from '../../import/application/import-scenario.command'; | ||
import { MarkExportAsFinished } from './mark-export-as-finished.command'; | ||
|
||
type CommandMapper = ( | ||
archiveLocation: string, | ||
ownerId: string, | ||
importResourceId: string, | ||
) => ICommand; | ||
|
||
@Injectable() | ||
export class ArchiveReadySaga { | ||
constructor(private readonly exportRepository: ExportRepository) {} | ||
|
||
private commandMapper: Record<ResourceKind, CommandMapper> = { | ||
project: ( | ||
archiveLocation: string, | ||
ownerId: string, | ||
importResourceId: string, | ||
) => | ||
new ImportProject( | ||
new ArchiveLocation(archiveLocation), | ||
new UserId(ownerId), | ||
new ResourceId(importResourceId), | ||
), | ||
scenario: ( | ||
archiveLocation: string, | ||
ownerId: string, | ||
importResourceId: string, | ||
) => | ||
new ImportScenario( | ||
new ArchiveLocation(archiveLocation), | ||
new UserId(ownerId), | ||
new ResourceId(importResourceId), | ||
), | ||
}; | ||
|
||
private async getCommands(event: ArchiveReady) { | ||
const exportInstance = await this.exportRepository.find(event.exportId); | ||
|
||
if (!exportInstance) throw new Error('cant find export'); | ||
|
||
const { | ||
resourceKind, | ||
archiveLocation, | ||
importResourceId, | ||
ownerId, | ||
} = exportInstance.toSnapshot(); | ||
|
||
if (!exportInstance.isClonning()) | ||
return [new MarkExportAsFinished(event.exportId)]; | ||
|
||
if (!archiveLocation || !importResourceId) | ||
throw new Error( | ||
'When clonning, the archiveLocation and importResourceId should be ready', | ||
); | ||
|
||
const importCommand = this.commandMapper[resourceKind]( | ||
archiveLocation, | ||
ownerId, | ||
importResourceId, | ||
); | ||
|
||
return [new MarkExportAsFinished(event.exportId), importCommand]; | ||
} | ||
|
||
@Saga() | ||
emitApiEvents = (events$: Observable<any>): Observable<ICommand> => | ||
events$.pipe( | ||
emitApiEvents = (events$: Observable<any>) => { | ||
return events$.pipe( | ||
ofType(ArchiveReady), | ||
map((event) => new MarkExportAsFinished(event.exportId)), | ||
mergeMap((event) => from(this.getCommands(event))), | ||
mergeMap((commands) => from(commands)), | ||
); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters