diff --git a/src/i18n/bg/errors.json b/src/i18n/bg/errors.json index f27fd21..7dc7954 100644 --- a/src/i18n/bg/errors.json +++ b/src/i18n/bg/errors.json @@ -65,7 +65,6 @@ "VIOLATION_CANNOT_CHANGE_STATUS_FROM_REJECTED_TO_REJECTED": "Сигналът не може да се промени от отхвърлен към отхвърлен", "VIOLATION_CANNOT_CHANGE_STATUS_FROM_RECEIVED_TO_RECEIVED": "Сигналът не може да се промени от получен към получен", "VIOLATION_CANNOT_CHANGE_STATUS_FROM_PROCESSING_TO_PROCESSING": "Сигналът не може да се промени от обработващ се към обработващ се", - "VIOLATION_SECTION_IN_DIFFERENT_TOWN": "Секцията не е в населеното място (общината) на сигнала", "VIOLATION_SECTION_NOT_FOUND": "Секция с този номер не съществува", "VIOLATION_SECTION_INVALID_FORMAT": "Номерът на секцията трябва да е точно 9 цифри", "VIOLATION_IS_ALREADY_PUBLISHED": "Сигналът вече е публикуван", diff --git a/src/i18n/en/errors.json b/src/i18n/en/errors.json index eb69a9a..0b4b953 100644 --- a/src/i18n/en/errors.json +++ b/src/i18n/en/errors.json @@ -65,7 +65,6 @@ "VIOLATION_CANNOT_CHANGE_STATUS_FROM_REJECTED_TO_REJECTED": "Violation cannot change status from rejected to rejected", "VIOLATION_CANNOT_CHANGE_STATUS_FROM_RECEIVED_TO_RECEIVED": "Violation cannot change status from received to received", "VIOLATION_CANNOT_CHANGE_STATUS_FROM_PROCESSING_TO_PROCESSING": "Violation cannot change status from processing to processing", - "VIOLATION_SECTION_IN_DIFFERENT_TOWN": "The section is not in the violation's town (municipality)", "VIOLATION_SECTION_NOT_FOUND": "There is no section with this number", "VIOLATION_SECTION_INVALID_FORMAT": "The section number must be exactly 9 digits", "VIOLATION_IS_ALREADY_PUBLISHED": "Violation is already published", diff --git a/src/violations/entities/violation-section.spec.ts b/src/violations/entities/violation-section.spec.ts index f8fcbdc..3268745 100644 --- a/src/violations/entities/violation-section.spec.ts +++ b/src/violations/entities/violation-section.spec.ts @@ -10,7 +10,12 @@ describe('Violation section', () => { const otherMunicipality = { id: 2, code: '01', name: 'Друга' } const town = (id: number, municipality = sofiaMunicipality): Town => - Object.assign(new Town(), { id, code: id * 10, municipality }) + Object.assign(new Town(), { + id, + code: id * 10, + name: `town-${id}`, + municipality, + }) const section = (id: string, sectionTown: Town): Section => Object.assign(new Section(), { id, town: sectionTown }) @@ -39,6 +44,8 @@ describe('Violation section', () => { expect(violation.updates[0].payload).toEqual({ from: null, to: '234602012', + fromTown: { code: 10, name: 'town-1' }, + toTown: { code: 10, name: 'town-1' }, }) }) @@ -47,7 +54,7 @@ describe('Violation section', () => { violation.setSection(actor, section('244601001', sofia)) expect(violation.section.id).toBe('244601001') - expect(violation.updates[0].payload).toEqual({ + expect(violation.updates[0].payload).toMatchObject({ from: '234602012', to: '244601001', }) @@ -69,25 +76,27 @@ describe('Violation section', () => { expect(violation.updates).toHaveLength(0) }) - it('refuses a section in another municipality', () => { - const violation = violationIn(sofia) - expect(() => - violation.setSection( - actor, - section('010100001', town(3, otherMunicipality)), - ), - ).toThrow(ViolationSectionException) - expect(violation.section).toBeUndefined() - expect(violation.updates).toHaveLength(0) + it('allows a section in another municipality and moves the town', () => { + const otherTown = town(3, otherMunicipality) + const violation = violationIn(sofia, section('234602012', sofia)) + violation.setSection(actor, section('010100001', otherTown)) + + expect(violation.section.id).toBe('010100001') + expect(violation.town).toBe(otherTown) + expect(violation.updates[0].payload).toEqual({ + from: '234602012', + to: '010100001', + fromTown: { code: 10, name: 'town-1' }, + toTown: { code: 30, name: 'town-3' }, + }) }) - it('refuses a section in another town abroad (no municipality)', () => { + it('allows moving a violation from a domestic town to a section abroad', () => { const abroad = town(4, null) - const otherAbroad = town(5, null) - const violation = violationIn(abroad) - expect(() => - violation.setSection(actor, section('320100001', otherAbroad)), - ).toThrow(ViolationSectionException) + const violation = violationIn(sofia) + violation.setSection(actor, section('320100001', abroad)) + + expect(violation.town).toBe(abroad) }) it('refuses a missing section', () => { diff --git a/src/violations/entities/violation-update.entity.ts b/src/violations/entities/violation-update.entity.ts index 02e67e9..cad09ac 100644 --- a/src/violations/entities/violation-update.entity.ts +++ b/src/violations/entities/violation-update.entity.ts @@ -8,6 +8,7 @@ import { } from 'typeorm' import { ulid } from 'ulid' import { User } from '../../users/entities' +import { Town } from '../../sections/entities/town.entity' import { Violation } from './violation.entity' export enum ViolationUpdateType { @@ -93,10 +94,17 @@ export class ViolationUpdate { actor: User, from: string | null, to: string, + fromTown?: Town | null, + toTown?: Town | null, ): ViolationUpdate { + const townRef = (town?: Town | null) => + town ? { code: town.code, name: town.name } : null + return ViolationUpdate.create(ViolationUpdateType.SECTION, actor, { from, to, + fromTown: townRef(fromTown), + toTown: townRef(toTown), }) } diff --git a/src/violations/entities/violation.entity.ts b/src/violations/entities/violation.entity.ts index 2c57996..2f053d2 100644 --- a/src/violations/entities/violation.entity.ts +++ b/src/violations/entities/violation.entity.ts @@ -209,9 +209,9 @@ export class Violation { /** * Set or correct the section, e.g. when the sender only picked a town that - * spans several election regions and wrote the section in the description. - * The section must be in the violation's town or in the same municipality - * (then the town follows the section). + * spans several election regions or picked the wrong town and wrote the + * section in the description. Any existing section is allowed; the town + * always follows the section. */ setSection(actor: User, section: Section): void { if (!section) { @@ -222,19 +222,18 @@ export class Violation { return } - const sameTown = !!this.town && section.town?.id === this.town.id - const sameMunicipality = - !!this.town?.municipality?.id && - section.town?.municipality?.id === this.town.municipality.id - if (!sameTown && !sameMunicipality) { - throw ViolationSectionException.forDifferentTown(this) - } - const previousSectionId = this.section?.id ?? null + const previousTown = this.town this.section = section this.town = section.town this.addUpdate( - ViolationUpdate.createSectionUpdate(actor, previousSectionId, section.id), + ViolationUpdate.createSectionUpdate( + actor, + previousSectionId, + section.id, + previousTown, + section.town, + ), ) } diff --git a/src/violations/entities/violation.exceptions.ts b/src/violations/entities/violation.exceptions.ts index bd5ca15..47a8cba 100644 --- a/src/violations/entities/violation.exceptions.ts +++ b/src/violations/entities/violation.exceptions.ts @@ -85,15 +85,6 @@ export class ViolationSectionException this.violation = violation } - public static forDifferentTown( - violation: Violation, - ): ViolationSectionException { - return new ViolationSectionException( - violation, - 'VIOLATION_SECTION_IN_DIFFERENT_TOWN', - ) - } - public static forMissingSection( violation: Violation, ): ViolationSectionException {