This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb42c01
commit 4443394
Showing
12 changed files
with
166 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Injectable } from '@nestjs/common' | ||
|
||
import { IncomeRepository } from '@back/money/domain/IncomeRepository' | ||
import { OutcomeRepository } from '@back/money/domain/OutcomeRepository' | ||
import { EntitySaver } from '@back/db/EntitySaver' | ||
|
||
@Injectable() | ||
export class TypoMerger { | ||
public constructor( | ||
private readonly incomeRepo: IncomeRepository, | ||
private readonly outcomeRepo: OutcomeRepository, | ||
private readonly entitySaver: EntitySaver, | ||
) {} | ||
|
||
public async merge( | ||
primary: string, | ||
secondary: string[], | ||
userLogin: string, | ||
): Promise<void> { | ||
await Promise.all([ | ||
this.mergeInIncomes(primary, secondary, userLogin), | ||
this.mergeInOutcomes(primary, secondary, userLogin), | ||
]) | ||
} | ||
|
||
private async mergeInIncomes( | ||
primary: string, | ||
secondary: string[], | ||
userLogin: string, | ||
): Promise<void> { | ||
const [mainIncomes, incomes] = await Promise.all([ | ||
this.incomeRepo.findBySourcesForUser([primary], userLogin), | ||
this.incomeRepo.findBySourcesForUser(secondary, userLogin), | ||
]) | ||
|
||
if (mainIncomes.length === 0) { | ||
return | ||
} | ||
|
||
incomes.forEach(income => { | ||
income.source = primary | ||
}) | ||
|
||
await this.entitySaver.save(...incomes) | ||
} | ||
|
||
private async mergeInOutcomes( | ||
primary: string, | ||
secondary: string[], | ||
userLogin: string, | ||
): Promise<void> { | ||
const [mainOutcomes, outcomes] = await Promise.all([ | ||
this.outcomeRepo.findByCategoriesForUser([primary], userLogin), | ||
this.outcomeRepo.findByCategoriesForUser(secondary, userLogin), | ||
]) | ||
|
||
if (mainOutcomes.length === 0) { | ||
return | ||
} | ||
|
||
outcomes.forEach(outcome => { | ||
outcome.category = primary | ||
}) | ||
|
||
await this.entitySaver.save(...outcomes) | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
back/src/mind/presentation/http/controller/TypoController.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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Controller, Get, Body, Post } from '@nestjs/common' | ||
import { | ||
ApiUseTags, | ||
ApiBearerAuth, | ||
ApiOperation, | ||
ApiOkResponse, | ||
} from '@nestjs/swagger' | ||
|
||
import { OnlyForUsers } from '@back/user/presentation/http/security/OnlyForUsers' | ||
import { TokenPayload } from '@back/user/application/dto/TokenPayload' | ||
import { CurrentUser } from '@back/user/presentation/http/decorator/CurrentUser' | ||
|
||
import { MergeTypoRequest } from '../request/MergeTypoRequest' | ||
import { TypoMerger } from '@back/mind/application/TypoMerger' | ||
|
||
@Controller('mind/typo') | ||
@OnlyForUsers() | ||
@ApiUseTags('mind') | ||
@ApiBearerAuth() | ||
export class TypoController { | ||
public constructor(private readonly merger: TypoMerger) {} | ||
|
||
@Post('merge') | ||
@ApiOperation({ title: 'Merge typos' }) | ||
@ApiOkResponse({ description: 'Merges' }) | ||
public async showAll( | ||
@CurrentUser() user: TokenPayload, | ||
@Body() request: MergeTypoRequest, | ||
): Promise<void> { | ||
await this.merger.merge(request.primary, request.secondary, user.login) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
back/src/mind/presentation/http/request/MergeTypoRequest.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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { ApiModelProperty } from '@nestjs/swagger' | ||
|
||
import { MergeTypoModel } from '@shared/models/mind/MergeTypoModel' | ||
|
||
export class MergeTypoRequest implements MergeTypoModel { | ||
@ApiModelProperty({ example: 'Lunch' }) | ||
public readonly primary: string | ||
|
||
@ApiModelProperty({ example: ['lunch', 'Lonch'] }) | ||
public readonly secondary: string[] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
import { MergeTypoModel } from '@shared/models/mind/MergeTypoModel' | ||
import { refetchData } from '@front/domain/money/actions/refetchData' | ||
import { fetchOrFail } from '@front/domain/store' | ||
|
||
import { actions } from '../reducer/tips' | ||
import { mergeTypoRequest } from '../api/mergeTypoRequest' | ||
|
||
export const mergeTypos = (token: string, main: string, other: string[]) => | ||
fetchOrFail(actions.fetching, async dispatch => { | ||
// TODO: real merging please =) | ||
console.log(main, other) | ||
export const mergeTypos = (token: string, merge: MergeTypoModel) => | ||
fetchOrFail(actions.fetching, async (dispatch, getApi) => { | ||
await mergeTypoRequest(getApi())(merge) | ||
|
||
dispatch(actions.data.removeTips([token])) | ||
|
||
await dispatch(refetchData()) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Api } from '@front/domain/api' | ||
import { MergeTypoModel } from '@shared/models/mind/MergeTypoModel' | ||
|
||
export const mergeTypoRequest = (api: Api) => ( | ||
merge: MergeTypoModel, | ||
): Promise<void> => | ||
api.client.post('/mind/typo/merge', merge).then(response => response.data) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface MergeTypoModel { | ||
primary: string | ||
secondary: string[] | ||
} |