From 2c47436259260a3e1d340ae64e69da1496a685ec Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 2 Jan 2021 19:56:04 +0100 Subject: [PATCH 1/5] Implemented a possible bugfix ref #68 --- src/models/entities/Address.ts | 13 +++---------- src/models/entities/GroupContact.ts | 5 +++-- src/models/entities/IAddressUser.ts | 11 +++++++++++ src/models/entities/Participant.ts | 5 +++-- src/models/entities/RunnerOrganisation.ts | 20 +++++++++++--------- 5 files changed, 31 insertions(+), 23 deletions(-) create mode 100644 src/models/entities/IAddressUser.ts diff --git a/src/models/entities/Address.ts b/src/models/entities/Address.ts index afa10da..561808f 100644 --- a/src/models/entities/Address.ts +++ b/src/models/entities/Address.ts @@ -7,7 +7,7 @@ import { } from "class-validator"; import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from "typeorm"; import { config } from '../../config'; -import { Participant } from "./Participant"; +import { IAddressUser } from './IAddressUser'; /** * Defines the Address entity. @@ -78,13 +78,6 @@ export class Address { /** * Used to link the address to participants. */ - @OneToMany(() => Participant, participant => participant.address, { nullable: true }) - participants: Participant[]; - - //TODO: #68 - // /** - // * Used to link the address to runner groups. - // */ - // @OneToMany(() => RunnerOrganisation, group => group.address, { nullable: true }) - // groups: RunnerOrganisation[]; + @OneToMany(() => IAddressUser, addressUser => addressUser.address, { nullable: true }) + addressUsers: IAddressUser[]; } diff --git a/src/models/entities/GroupContact.ts b/src/models/entities/GroupContact.ts index f524863..4dbcc5e 100644 --- a/src/models/entities/GroupContact.ts +++ b/src/models/entities/GroupContact.ts @@ -10,6 +10,7 @@ import { import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm"; import { config } from '../../config'; import { Address } from "./Address"; +import { IAddressUser } from './IAddressUser'; import { RunnerGroup } from "./RunnerGroup"; /** @@ -17,7 +18,7 @@ import { RunnerGroup } from "./RunnerGroup"; * Mainly it's own class to reduce duplicate code and enable contact's to be associated with multiple groups. */ @Entity() -export class GroupContact { +export class GroupContact implements IAddressUser { /** * Autogenerated unique id (primary key). */ @@ -54,7 +55,7 @@ export class GroupContact { * This is a address object to prevent any formatting differences. */ @IsOptional() - @ManyToOne(() => Address, address => address.participants, { nullable: true }) + @ManyToOne(() => Address, address => address.addressUsers, { nullable: true }) address?: Address; /** diff --git a/src/models/entities/IAddressUser.ts b/src/models/entities/IAddressUser.ts new file mode 100644 index 0000000..425f4e3 --- /dev/null +++ b/src/models/entities/IAddressUser.ts @@ -0,0 +1,11 @@ +import { Entity, ManyToOne, PrimaryColumn } from 'typeorm'; +import { Address } from './Address'; + +@Entity() +export abstract class IAddressUser { + @PrimaryColumn() + id: number; + + @ManyToOne(() => Address, address => address.addressUsers, { nullable: true }) + address?: Address +} diff --git a/src/models/entities/Participant.ts b/src/models/entities/Participant.ts index 19af17e..de13b5f 100644 --- a/src/models/entities/Participant.ts +++ b/src/models/entities/Participant.ts @@ -10,6 +10,7 @@ import { import { Column, Entity, ManyToOne, PrimaryGeneratedColumn, TableInheritance } from "typeorm"; import { config } from '../../config'; import { Address } from "./Address"; +import { IAddressUser } from './IAddressUser'; /** * Defines the Participant entity. @@ -17,7 +18,7 @@ import { Address } from "./Address"; */ @Entity() @TableInheritance({ column: { name: "type", type: "varchar" } }) -export abstract class Participant { +export abstract class Participant implements IAddressUser { /** * Autogenerated unique id (primary key). */ @@ -53,7 +54,7 @@ export abstract class Participant { * The participant's address. * This is a address object to prevent any formatting differences. */ - @ManyToOne(() => Address, address => address.participants, { nullable: true }) + @ManyToOne(() => Address, address => address.addressUsers, { nullable: true }) address?: Address; /** diff --git a/src/models/entities/RunnerOrganisation.ts b/src/models/entities/RunnerOrganisation.ts index 7110b09..f0f8c78 100644 --- a/src/models/entities/RunnerOrganisation.ts +++ b/src/models/entities/RunnerOrganisation.ts @@ -1,5 +1,7 @@ -import { IsInt } from "class-validator"; -import { ChildEntity, OneToMany } from "typeorm"; +import { IsInt, IsOptional } from "class-validator"; +import { ChildEntity, ManyToOne, OneToMany } from "typeorm"; +import { Address } from './Address'; +import { IAddressUser } from './IAddressUser'; import { Runner } from './Runner'; import { RunnerGroup } from "./RunnerGroup"; import { RunnerTeam } from "./RunnerTeam"; @@ -9,14 +11,14 @@ import { RunnerTeam } from "./RunnerTeam"; * This usually is a school, club or company. */ @ChildEntity() -export class RunnerOrganisation extends RunnerGroup { +export class RunnerOrganisation extends RunnerGroup implements IAddressUser { - // /** - // * The organisations's address. - // */ - // @IsOptional() - // @ManyToOne(() => Address, address => address.groups, { nullable: true }) - // address?: Address; + /** + * The organisations's address. + */ + @IsOptional() + @ManyToOne(() => Address, address => address.addressUsers, { nullable: true }) + address?: Address; /** * The organisation's teams. From 9c4e54fc6e738194475c956895a229b18b51a3f4 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 2 Jan 2021 19:57:33 +0100 Subject: [PATCH 2/5] Added comments to the bugfix --- src/models/entities/IAddressUser.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/models/entities/IAddressUser.ts b/src/models/entities/IAddressUser.ts index 425f4e3..56ee840 100644 --- a/src/models/entities/IAddressUser.ts +++ b/src/models/entities/IAddressUser.ts @@ -1,6 +1,10 @@ import { Entity, ManyToOne, PrimaryColumn } from 'typeorm'; import { Address } from './Address'; +/** + * The interface(-ish) all entities using addresses have to implement. + * This is a abstract class, because apparently typeorm can't really work with interfaces :/ + */ @Entity() export abstract class IAddressUser { @PrimaryColumn() From 56c6a7efb057da6a43081be803ae2b15402eb2fd Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 2 Jan 2021 19:57:55 +0100 Subject: [PATCH 3/5] Revert "Removed addresses from tests until the circular dependencies are solved" This reverts commit 599296c4e3736bf9aadbc32067cd2ff8c39f0f17. --- src/controllers/RunnerOrganisationController.ts | 10 +++++----- src/tests/runnerOrgs/org_add.spec.ts | 4 ++-- src/tests/runnerOrgs/org_delete.spec.ts | 4 ++-- src/tests/runnerOrgs/org_update.spec.ts | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/controllers/RunnerOrganisationController.ts b/src/controllers/RunnerOrganisationController.ts index be25805..e10d415 100644 --- a/src/controllers/RunnerOrganisationController.ts +++ b/src/controllers/RunnerOrganisationController.ts @@ -29,7 +29,7 @@ export class RunnerOrganisationController { @OpenAPI({ description: 'Lists all organisations.
This includes their address, contact and teams (if existing/associated).' }) async getAll() { let responseTeams: ResponseRunnerOrganisation[] = new Array(); - const runners = await this.runnerOrganisationRepository.find({ relations: [/*'address',*/ 'contact', 'teams'] }); + const runners = await this.runnerOrganisationRepository.find({ relations: ['address', 'contact', 'teams'] }); runners.forEach(runner => { responseTeams.push(new ResponseRunnerOrganisation(runner)); }); @@ -43,7 +43,7 @@ export class RunnerOrganisationController { @OnUndefined(RunnerOrganisationNotFoundError) @OpenAPI({ description: 'Lists all information about the organisation whose id got provided.' }) async getOne(@Param('id') id: number) { - let runnerOrg = await this.runnerOrganisationRepository.findOne({ id: id }, { relations: [/*'address',*/ 'contact', 'teams'] }); + let runnerOrg = await this.runnerOrganisationRepository.findOne({ id: id }, { relations: ['address', 'contact', 'teams'] }); if (!runnerOrg) { throw new RunnerOrganisationNotFoundError(); } return new ResponseRunnerOrganisation(runnerOrg); } @@ -62,7 +62,7 @@ export class RunnerOrganisationController { runnerOrganisation = await this.runnerOrganisationRepository.save(runnerOrganisation); - return new ResponseRunnerOrganisation(await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: [/*'address',*/ 'contact', 'teams'] })); + return new ResponseRunnerOrganisation(await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['address', 'contact', 'teams'] })); } @Put('/:id') @@ -84,7 +84,7 @@ export class RunnerOrganisationController { await this.runnerOrganisationRepository.save(await updateOrganisation.updateRunnerOrganisation(oldRunnerOrganisation)); - return new ResponseRunnerOrganisation(await this.runnerOrganisationRepository.findOne(id, { relations: [/*'address',*/ 'contact', 'teams'] })); + return new ResponseRunnerOrganisation(await this.runnerOrganisationRepository.findOne(id, { relations: ['address', 'contact', 'teams'] })); } @Delete('/:id') @@ -98,7 +98,7 @@ export class RunnerOrganisationController { async remove(@Param("id") id: number, @QueryParam("force") force: boolean) { let organisation = await this.runnerOrganisationRepository.findOne({ id: id }); if (!organisation) { return null; } - let runnerOrganisation = await this.runnerOrganisationRepository.findOne(organisation, { relations: [/*'address',*/ 'contact', 'runners', 'teams'] }); + let runnerOrganisation = await this.runnerOrganisationRepository.findOne(organisation, { relations: ['address', 'contact', 'runners', 'teams'] }); if (!force) { if (runnerOrganisation.teams.length != 0) { diff --git a/src/tests/runnerOrgs/org_add.spec.ts b/src/tests/runnerOrgs/org_add.spec.ts index 62aa067..6ee0606 100644 --- a/src/tests/runnerOrgs/org_add.spec.ts +++ b/src/tests/runnerOrgs/org_add.spec.ts @@ -56,7 +56,7 @@ describe('adding + getting from all orgs', () => { expect(added_org).toEqual({ "name": "test123", "contact": null, - // "address": null, + "address": null, "teams": [] }) }); @@ -83,7 +83,7 @@ describe('adding + getting explicitly', () => { expect(added_org2).toEqual({ "name": "test123", "contact": null, - // "address": null, + "address": null, "teams": [] }) }); diff --git a/src/tests/runnerOrgs/org_delete.spec.ts b/src/tests/runnerOrgs/org_delete.spec.ts index 9598704..08891f8 100644 --- a/src/tests/runnerOrgs/org_delete.spec.ts +++ b/src/tests/runnerOrgs/org_delete.spec.ts @@ -44,7 +44,7 @@ describe('adding + deletion (successfull)', () => { expect(added_org2).toEqual({ "name": "test123", "contact": null, - // "address": null, + "address": null, "teams": [] }); }); @@ -121,7 +121,7 @@ describe('adding + deletion with teams still existing (with force)', () => { expect(added_org2).toEqual({ "name": "test123", "contact": null, - // "address": null + "address": null }); }); it('check if org really was deleted', async () => { diff --git a/src/tests/runnerOrgs/org_update.spec.ts b/src/tests/runnerOrgs/org_update.spec.ts index a410521..e6a1055 100644 --- a/src/tests/runnerOrgs/org_update.spec.ts +++ b/src/tests/runnerOrgs/org_update.spec.ts @@ -32,7 +32,7 @@ describe('adding + updating name', () => { "id": added_org_id, "name": "testlelele", "contact": null, - // "address": null, + "address": null, }, axios_config); expect(res2.status).toEqual(200); expect(res2.headers['content-type']).toContain("application/json") @@ -42,7 +42,7 @@ describe('adding + updating name', () => { expect(added_org2).toEqual({ "name": "testlelele", "contact": null, - // "address": null, + "address": null, "teams": [] }) }); @@ -65,7 +65,7 @@ describe('adding + try updating id (should return 406)', () => { "id": added_org_id + 1, "name": "testlelele", "contact": null, - // "address": null, + "address": null, }, axios_config); expect(res2.status).toEqual(406); expect(res2.headers['content-type']).toContain("application/json") From 1e2de7656e36405b9b15d4e0a7004e8b14e61c85 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 2 Jan 2021 20:03:02 +0100 Subject: [PATCH 4/5] Reenabled addresses in org responses ref #68 --- src/models/responses/ResponseRunnerOrganisation.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/models/responses/ResponseRunnerOrganisation.ts b/src/models/responses/ResponseRunnerOrganisation.ts index 8e31a71..69ccaf1 100644 --- a/src/models/responses/ResponseRunnerOrganisation.ts +++ b/src/models/responses/ResponseRunnerOrganisation.ts @@ -1,7 +1,8 @@ import { IsArray, - IsNotEmpty, - IsObject + + IsObject, + IsOptional } from "class-validator"; import { Address } from '../entities/Address'; import { RunnerOrganisation } from '../entities/RunnerOrganisation'; @@ -17,7 +18,7 @@ export class ResponseRunnerOrganisation extends ResponseRunnerGroup { * The runnerOrganisation's address. */ @IsObject() - @IsNotEmpty() + @IsOptional() address?: Address; /** @@ -32,7 +33,7 @@ export class ResponseRunnerOrganisation extends ResponseRunnerGroup { */ public constructor(org: RunnerOrganisation) { super(org); - // this.address = org.address; + this.address = org.address; this.teams = org.teams; } } From 97e8470b0d9c2d6e87fb951236769eb4274e2ab9 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 2 Jan 2021 21:53:21 +0100 Subject: [PATCH 5/5] Change requested by @philipp ref #70 --- src/models/entities/IAddressUser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/models/entities/IAddressUser.ts b/src/models/entities/IAddressUser.ts index 56ee840..3a7762a 100644 --- a/src/models/entities/IAddressUser.ts +++ b/src/models/entities/IAddressUser.ts @@ -2,7 +2,7 @@ import { Entity, ManyToOne, PrimaryColumn } from 'typeorm'; import { Address } from './Address'; /** - * The interface(-ish) all entities using addresses have to implement. + * The interface(tm) all entities using addresses have to implement. * This is a abstract class, because apparently typeorm can't really work with interfaces :/ */ @Entity()