Skip to content
Snippets Groups Projects
Commit 01eb4c45 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-5010: update bescheid draft

parent 107bb1ea
No related branches found
No related tags found
No related merge requests found
export enum BescheidLinkRel { export enum BescheidLinkRel {
UPLOAD_BESCHEID_FILE = 'uploadBescheidFile', UPLOAD_BESCHEID_FILE = 'uploadBescheidFile',
UPLOAD_ATTACHMENT = 'uploadAttachment', UPLOAD_ATTACHMENT = 'uploadAttachment',
UPDATE = 'update',
CREATE_BESCHEID_DOCUMENT = 'createBescheidDocument',
} }
...@@ -15,13 +15,23 @@ import { ...@@ -15,13 +15,23 @@ import {
StateResource, StateResource,
} from '@alfa-client/tech-shared'; } from '@alfa-client/tech-shared';
import { ResourceRepository } from '../../../tech-shared/src/lib/resource/resource.repository'; import { ResourceRepository } from '../../../tech-shared/src/lib/resource/resource.repository';
import { CommandOrder, CommandService, CreateCommand } from '@alfa-client/command-shared'; import {
CommandOrder,
CommandResource,
CommandService,
CreateCommand,
CreateCommandProps,
} from '@alfa-client/command-shared';
import faker from '@faker-js/faker'; import faker from '@faker-js/faker';
import { createBescheidResource } from '../test/bescheid'; import { createBescheid, createBescheidResource } from '../test/bescheid';
import { createCommandStateResource } from '../../../command-shared/test/command'; import {
createCommandResource,
createCommandStateResource,
} from '../../../command-shared/test/command';
import { singleCold } from '../../../tech-shared/src/lib/resource/marbles'; import { singleCold } from '../../../tech-shared/src/lib/resource/marbles';
import { BescheidResource } from './bescheid.model'; import { Bescheid, BescheidResource } from './bescheid.model';
import { cold } from 'jest-marbles'; import { cold } from 'jest-marbles';
import { BescheidLinkRel } from '@alfa-client/bescheid-shared';
describe('BescheidService', () => { describe('BescheidService', () => {
let service: BescheidService; let service: BescheidService;
...@@ -224,4 +234,40 @@ describe('BescheidService', () => { ...@@ -224,4 +234,40 @@ describe('BescheidService', () => {
expect(createdCommand).toEqual(command); expect(createdCommand).toEqual(command);
}); });
}); });
describe('updateBescheid', () => {
let bescheidResource: BescheidResource;
let bescheid: Bescheid;
let updateBescheidCommandResource: CommandResource;
beforeEach(() => {
bescheidResource = createBescheidResource();
bescheid = createBescheid();
updateBescheidCommandResource = createCommandResource();
commandService.createCommandByProps.mockReturnValue(
of(createStateResource(updateBescheidCommandResource)),
);
});
it('should create command', () => {
service.updateBescheid(bescheidResource, bescheid);
expect(commandService.createCommandByProps).toHaveBeenCalledWith({
resource: bescheidResource,
linkRel: BescheidLinkRel.UPDATE,
command: {
order: CommandOrder.UPDATE_BESCHEID,
body: bescheid,
},
} as CreateCommandProps);
});
it('should return command', () => {
const updateBescheid$ = service.updateBescheid(bescheidResource, bescheid);
expect(updateBescheid$).toBeObservable(
cold('(a|)', { a: createStateResource(updateBescheidCommandResource) }),
);
});
});
}); });
...@@ -13,6 +13,7 @@ import { ResourceService } from '../../../tech-shared/src/lib/resource/resource. ...@@ -13,6 +13,7 @@ import { ResourceService } from '../../../tech-shared/src/lib/resource/resource.
import { BescheidFacade } from './+state/bescheid.facade'; import { BescheidFacade } from './+state/bescheid.facade';
import { Bescheid, BescheidResource } from './bescheid.model'; import { Bescheid, BescheidResource } from './bescheid.model';
import { buildCreateBescheidCommand } from './bescheid.util'; import { buildCreateBescheidCommand } from './bescheid.util';
import { BescheidLinkRel } from './bescheid.linkrel';
@Injectable({ providedIn: 'root' }) @Injectable({ providedIn: 'root' })
export class BescheidService { export class BescheidService {
...@@ -84,4 +85,18 @@ export class BescheidService { ...@@ -84,4 +85,18 @@ export class BescheidService {
}, },
); );
} }
public updateBescheid(
bescheidResource: BescheidResource,
bescheid: Bescheid,
): Observable<StateResource<CommandResource>> {
return this.commandService.createCommandByProps({
resource: bescheidResource,
linkRel: BescheidLinkRel.UPDATE,
command: {
order: CommandOrder.UPDATE_BESCHEID,
body: bescheid,
},
});
}
} }
...@@ -82,6 +82,8 @@ export enum CommandOrder { ...@@ -82,6 +82,8 @@ export enum CommandOrder {
CREATE_BESCHEID = 'CREATE_BESCHEID', CREATE_BESCHEID = 'CREATE_BESCHEID',
SET_AKTENZEICHEN = 'SET_AKTENZEICHEN', SET_AKTENZEICHEN = 'SET_AKTENZEICHEN',
DELETE_BESCHEID = 'DELETE_BESCHEID', DELETE_BESCHEID = 'DELETE_BESCHEID',
UPDATE_BESCHEID = 'UPDATE_BESCHEID',
CREATE_BESCHEID_DOCUMENT_FROM_FILE = 'CREATE_BESCHEID_DOCUMENT_FROM_FILE',
} }
export enum CommandOrderType { export enum CommandOrderType {
......
import { Bescheid, BescheidService } from '@alfa-client/bescheid-shared'; import {
import { createEmptyStateResource, formatForDatabase } from '@alfa-client/tech-shared'; Bescheid,
BescheidLinkRel,
BescheidResource,
BescheidService,
} from '@alfa-client/bescheid-shared';
import { createStateResource, formatForDatabase, StateResource } from '@alfa-client/tech-shared';
import { Mock, mock, useFromMock } from '@alfa-client/test-utils'; import { Mock, mock, useFromMock } from '@alfa-client/test-utils';
import { registerLocaleData } from '@angular/common'; import { registerLocaleData } from '@angular/common';
import localeDe from '@angular/common/locales/de'; import localeDe from '@angular/common/locales/de';
import { UntypedFormBuilder } from '@angular/forms'; import { UntypedFormBuilder } from '@angular/forms';
import { cold } from 'jest-marbles'; import { cold } from 'jest-marbles';
import { of } from 'rxjs'; import { of } from 'rxjs';
import { createBescheid } from '../../../../../bescheid-shared/src/test/bescheid'; import {
createBescheid,
createBescheidResource,
} from '../../../../../bescheid-shared/src/test/bescheid';
import { createVorgangWithEingangResource } from '../../../../../vorgang-shared/test/vorgang'; import { createVorgangWithEingangResource } from '../../../../../vorgang-shared/test/vorgang';
import { BescheidenFormService } from './bescheiden.formservice'; import { BescheidenFormService } from './bescheiden.formservice';
import {
VorgangService,
VorgangWithEingangLinkRel,
VorgangWithEingangResource,
} from '@alfa-client/vorgang-shared';
import {
createCommandResource,
createCommandStateResource,
} from '../../../../../command-shared/test/command';
import { CommandResource } from '@alfa-client/command-shared';
registerLocaleData(localeDe); registerLocaleData(localeDe);
describe('BescheidenFormServiceService', () => { describe('BescheidenFormServiceService', () => {
let service: BescheidenFormService; let service: BescheidenFormService;
let bescheidService: Mock<BescheidService>; let bescheidService: Mock<BescheidService>;
let vorgangService: Mock<VorgangService>;
const now = new Date(); const now = new Date();
Date.now = jest.fn().mockReturnValue(now); Date.now = jest.fn().mockReturnValue(now);
beforeEach(() => { beforeEach(() => {
bescheidService = mock(BescheidService); bescheidService = mock(BescheidService);
service = new BescheidenFormService(new UntypedFormBuilder(), useFromMock(bescheidService)); vorgangService = mock(VorgangService);
service = new BescheidenFormService(
new UntypedFormBuilder(),
useFromMock(bescheidService),
useFromMock(vorgangService),
);
}); });
describe('initializeFormChanges', () => { describe('initializeFormChanges', () => {
...@@ -95,50 +119,134 @@ describe('BescheidenFormServiceService', () => { ...@@ -95,50 +119,134 @@ describe('BescheidenFormServiceService', () => {
}); });
}); });
describe('submit', () => { describe('createOrUpdateBescheid', () => {
let getValue; let getValue: jest.Mock;
const vorgangWithEingangResource = createVorgangWithEingangResource(); let vorgangWithEingangResource: VorgangWithEingangResource;
const value = createBescheid(); const formValue: Bescheid = createBescheid();
const bescheidCommandStateResource = createEmptyStateResource(); const bescheidCommandStateResource: StateResource<CommandResource> =
createCommandStateResource();
beforeEach(() => { beforeEach(() => {
getValue = service.getValue = jest.fn(); getValue = service.getValue = jest.fn();
getValue.mockReturnValue(value); getValue.mockReturnValue(formValue);
bescheidService.getBescheidCommand.mockReturnValue(of(bescheidCommandStateResource)); });
describe('on new bescheid draft', () => {
beforeEach(() => {
vorgangWithEingangResource = createVorgangWithEingangResource([
VorgangWithEingangLinkRel.CREATE_BESCHEID_DRAFT,
]);
service.setVorgangWithEingangResource(vorgangWithEingangResource); service.setVorgangWithEingangResource(vorgangWithEingangResource);
bescheidService.getBescheidCommand.mockReturnValue(of(bescheidCommandStateResource));
}); });
it('should get value', (done) => { it('should get value', (done) => {
service.submit().subscribe(() => { service.createOrUpdateBescheid(vorgangWithEingangResource).subscribe(() => {
expect(getValue).toHaveBeenCalled(); expect(getValue).toHaveBeenCalled();
done(); done();
}); });
}); });
it('should create bescheid', (done) => { it('should create bescheid', (done) => {
service.submit().subscribe(() => { service.createOrUpdateBescheid(vorgangWithEingangResource).subscribe(() => {
expect(bescheidService.createBescheid).toHaveBeenCalledWith( expect(bescheidService.createBescheid).toHaveBeenCalledWith(
vorgangWithEingangResource, vorgangWithEingangResource,
value, formValue,
); );
done(); done();
}); });
}); });
it('should get bescheid command', (done) => { it('should get bescheid command', (done) => {
service.submit().subscribe(() => { service.createOrUpdateBescheid(vorgangWithEingangResource).subscribe(() => {
expect(bescheidService.getBescheidCommand).toHaveBeenCalledWith(); expect(bescheidService.getBescheidCommand).toHaveBeenCalledWith();
done(); done();
}); });
}); });
it('should return bescheid command', () => { it('should return bescheid command', () => {
const bescheidCommand$ = service.submit(); const bescheidCommand$ = service.createOrUpdateBescheid(vorgangWithEingangResource);
expect(bescheidCommand$).toBeObservable(cold('(a|)', { a: bescheidCommandStateResource })); expect(bescheidCommand$).toBeObservable(cold('(a|)', { a: bescheidCommandStateResource }));
}); });
}); });
describe('on existing bescheid draft', () => {
let bescheidResource: BescheidResource;
let updateBescheidCommandStateResource: StateResource<CommandResource>;
beforeEach(() => {
vorgangWithEingangResource = createVorgangWithEingangResource([
VorgangWithEingangLinkRel.BESCHEID_DRAFT,
]);
service.setVorgangWithEingangResource(vorgangWithEingangResource);
bescheidResource = createBescheidResource([BescheidLinkRel.UPDATE]);
bescheidService.getBescheidDraft.mockReturnValue(of(createStateResource(bescheidResource)));
updateBescheidCommandStateResource = createCommandStateResource();
bescheidService.updateBescheid.mockReturnValue(of(updateBescheidCommandStateResource));
});
it('should get bescheid draft', (done) => {
service.createOrUpdateBescheid(vorgangWithEingangResource).subscribe(() => {
expect(bescheidService.getBescheidDraft).toHaveBeenCalled();
done();
});
});
it('should update bescheid draft', (done) => {
service.createOrUpdateBescheid(vorgangWithEingangResource).subscribe(() => {
expect(bescheidService.updateBescheid).toHaveBeenCalledWith(bescheidResource, formValue);
done();
});
});
it('should return command', () => {
const submit$ = service.createOrUpdateBescheid(vorgangWithEingangResource);
expect(submit$).toBeObservable(cold('(a|)', { a: updateBescheidCommandStateResource }));
});
});
});
describe('submit', () => {
let vorgangWithEingangResource: VorgangWithEingangResource;
let createOrUpdateCommandResource: CommandResource;
let createOrUpdateBescheid: jest.Mock;
beforeEach(() => {
vorgangWithEingangResource = createVorgangWithEingangResource();
vorgangService.getVorgangWithEingang.mockReturnValue(
of(createStateResource(vorgangWithEingangResource)),
);
createOrUpdateCommandResource = createCommandResource();
createOrUpdateBescheid = service.createOrUpdateBescheid = jest.fn();
createOrUpdateBescheid.mockReturnValue(
of(createStateResource(createOrUpdateCommandResource)),
);
});
it('should get vorgang with eingang', () => {
service.submit();
expect(vorgangService.getVorgangWithEingang).toHaveBeenCalled();
});
it('should create or update bescheid', (done) => {
service.submit().subscribe(() => {
expect(createOrUpdateBescheid).toHaveBeenCalledWith(vorgangWithEingangResource);
done();
});
});
it('should return create or update bescheid command', () => {
const submit$ = service.submit();
expect(submit$).toBeObservable(
cold('(a|)', { a: createStateResource(createOrUpdateCommandResource) }),
);
});
});
describe('setVorgangWithEingangResource', () => { describe('setVorgangWithEingangResource', () => {
it('should set vorgangWithEingangResource', () => { it('should set vorgangWithEingangResource', () => {
const vorgangWithEingangResource = createVorgangWithEingangResource(); const vorgangWithEingangResource = createVorgangWithEingangResource();
......
import { Bescheid, BescheidService } from '@alfa-client/bescheid-shared'; import { Bescheid, BescheidResource, BescheidService } from '@alfa-client/bescheid-shared';
import { BinaryFileResource } from '@alfa-client/binary-file-shared'; import { BinaryFileResource } from '@alfa-client/binary-file-shared';
import { import {
AbstractFormService, AbstractFormService,
StateResource,
asBoolean, asBoolean,
createEmptyStateResource, createEmptyStateResource,
formatForDatabase, formatForDatabase,
HttpError,
isLoaded,
notHasLink,
StateResource,
} from '@alfa-client/tech-shared'; } from '@alfa-client/tech-shared';
import { VorgangWithEingangResource } from '@alfa-client/vorgang-shared'; import {
VorgangService,
VorgangWithEingangLinkRel,
VorgangWithEingangResource,
} from '@alfa-client/vorgang-shared';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { import {
UntypedFormArray, UntypedFormArray,
...@@ -16,7 +23,9 @@ import { ...@@ -16,7 +23,9 @@ import {
UntypedFormGroup, UntypedFormGroup,
} from '@angular/forms'; } from '@angular/forms';
import { isNil } from 'lodash-es'; import { isNil } from 'lodash-es';
import { BehaviorSubject, Observable, map, of, startWith } from 'rxjs'; import { BehaviorSubject, filter, map, Observable, of, startWith, switchMap } from 'rxjs';
import { hasLink, Resource } from '@ngxp/rest';
import { CommandResource } from '@alfa-client/command-shared';
@Injectable() @Injectable()
export class BescheidenFormService extends AbstractFormService { export class BescheidenFormService extends AbstractFormService {
...@@ -35,7 +44,8 @@ export class BescheidenFormService extends AbstractFormService { ...@@ -35,7 +44,8 @@ export class BescheidenFormService extends AbstractFormService {
constructor( constructor(
formBuilder: UntypedFormBuilder, formBuilder: UntypedFormBuilder,
private bescheidService: BescheidService, private readonly bescheidService: BescheidService,
private readonly vorgangService: VorgangService,
) { ) {
super(formBuilder); super(formBuilder);
this.bescheidChanges$ = new BehaviorSubject<Bescheid>(this.getFormValue()); this.bescheidChanges$ = new BehaviorSubject<Bescheid>(this.getFormValue());
...@@ -82,11 +92,39 @@ export class BescheidenFormService extends AbstractFormService { ...@@ -82,11 +92,39 @@ export class BescheidenFormService extends AbstractFormService {
return BescheidenFormService.FIELD_PATH_PREFIX; return BescheidenFormService.FIELD_PATH_PREFIX;
} }
protected doSubmit(): Observable<StateResource<any>> { protected doSubmit(): Observable<StateResource<Resource | HttpError>> {
// TODO Mit this.isPatch() unterscheiden, ob create oder update notwendig ist // TODO Mit this.isPatch() unterscheiden, ob create oder update notwendig ist
this.bescheidService.createBescheid(this.vorgangWithEingangResource, this.getValue()); return this.vorgangService.getVorgangWithEingang().pipe(
filter(isLoaded),
map((stateResource) => stateResource.resource),
switchMap((vorgangWithEingangResource: VorgangWithEingangResource) =>
this.createOrUpdateBescheid(vorgangWithEingangResource),
),
);
}
createOrUpdateBescheid(
vorgangWithEingangResource: VorgangWithEingangResource,
): Observable<StateResource<CommandResource>> {
if (this.isNewBescheidDraft(vorgangWithEingangResource)) {
this.bescheidService.createBescheid(vorgangWithEingangResource, this.getValue());
return this.bescheidService.getBescheidCommand(); return this.bescheidService.getBescheidCommand();
} }
return this.bescheidService.getBescheidDraft().pipe(
filter(isLoaded),
map((stateResource: StateResource<BescheidResource>) => stateResource.resource),
switchMap((bescheidResource) =>
this.bescheidService.updateBescheid(bescheidResource, this.getValue()),
),
);
}
private isNewBescheidDraft(vorgangWithEingangResource: VorgangWithEingangResource): boolean {
return (
hasLink(vorgangWithEingangResource, VorgangWithEingangLinkRel.CREATE_BESCHEID_DRAFT) &&
notHasLink(vorgangWithEingangResource, VorgangWithEingangLinkRel.BESCHEID_DRAFT)
);
}
public getValue(): Bescheid { public getValue(): Bescheid {
const value = this.getFormValue(); const value = this.getFormValue();
......
...@@ -73,6 +73,8 @@ public enum CommandOrder { ...@@ -73,6 +73,8 @@ public enum CommandOrder {
CREATE_BESCHEID(false, Type.VORGANG), CREATE_BESCHEID(false, Type.VORGANG),
DELETE_BESCHEID(false, Type.VORGANG), DELETE_BESCHEID(false, Type.VORGANG),
UPDATE_BESCHEID(false, Type.VORGANG),
CREATE_BESCHEID_DOCUMENT_FROM_FILE(false, Type.VORGANG),
PROCESS_VORGANG(false, Type.VORGANG); PROCESS_VORGANG(false, Type.VORGANG);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment