Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/i18n/bg/errors.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "Сигналът вече е публикуван",
Expand Down
1 change: 0 additions & 1 deletion src/i18n/en/errors.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
45 changes: 27 additions & 18 deletions src/violations/entities/violation-section.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down Expand Up @@ -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' },
})
})

Expand All @@ -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',
})
Expand All @@ -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', () => {
Expand Down
8 changes: 8 additions & 0 deletions src/violations/entities/violation-update.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
})
}

Expand Down
23 changes: 11 additions & 12 deletions src/violations/entities/violation.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
),
)
}

Expand Down
9 changes: 0 additions & 9 deletions src/violations/entities/violation.exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading