Skip to content

Commit

Permalink
[#159] Schema changed, refactored services, added property sector ent…
Browse files Browse the repository at this point in the history
…ity and classes (api still has a small bug)
  • Loading branch information
JamalG16 committed Nov 29, 2019
1 parent 078673b commit 87b6e92
Show file tree
Hide file tree
Showing 26 changed files with 440 additions and 225 deletions.
20 changes: 12 additions & 8 deletions backend/src/constants/BodyFields.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
const UserFields = {
loginFields : ['email', 'password'],
createFields : ['email', 'password', 'firstName', 'lastName', 'phoneNumber', 'userType']
}
createFields : ['email', 'password', 'firstName', 'lastName', 'phoneNumber', 'userType'],
};

const WorkOrderFields = {
createFields : ['sectorType', 'workOrderType', 'title', 'cause', 'serviceNeeded',
'priorityType', 'description', 'dueDate', 'priceEstimate']
}
createFields : ['sectorKind', 'workOrderType', 'title', 'cause', 'serviceNeeded',
'priorityType', 'description', 'dueDate', 'priceEstimate'],
};

const PropertyFields = {
createFields : ['name', 'propertyType', 'address']
}
createFields : ['name', 'propertyType', 'address'],
};

export {UserFields, WorkOrderFields, PropertyFields};
const PROPERTY_SECTOR_FIELDS = {
createFields : ['sectorKind'],
};

export { UserFields, WorkOrderFields, PropertyFields, PROPERTY_SECTOR_FIELDS };
108 changes: 56 additions & 52 deletions backend/src/constants/FindOptionsFields.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,71 @@
import { FindOptions } from "typeorm";
import { Property } from "../entities/Property";
import { User } from "../entities/User";
import { WorkOrder } from "../entities/WorkOrder";
import { FindOptions } from 'typeorm';
import { Property } from '../entities/Property';
import { User } from '../entities/User';
import { WorkOrder } from '../entities/WorkOrder';

const PropertyFields : FindOptions<Property> = {
relations: ['activityStatus', 'propertyType', 'user'],
select: {
id: true,
name: true,
id: true,
name: true,
user: {
id: true
},
id: true,
},
address: true,
activityStatus: {
id: true,
status: true
},
id: true,
status: true,
},
propertyType: {
id: true,
type: true
}
}
}
id: true,
type: true,
},
},
};

const PropertyFieldsNoUser : FindOptions<Property> = {
relations: ['activityStatus', 'propertyType'],
select: {
id: true,
name: true,
id: true,
name: true,
address: true,
activityStatus: {
id: true,
status: true
},
id: true,
status: true,
},
propertyType: {
id: true,
type: true
}
}
}
id: true,
type: true,
},
},
};

const UserFields : FindOptions<User> = {
relations: ['userType'],
select: {
id: true,
firstName: true,
id: true,
firstName: true,
lastName: true,
email: true,
userType: {
id: true,
type: true
}
}
}
type: true,
},
},
};

const WorkOrderFields : FindOptions<WorkOrder> = {
relations: ['workOrderType', 'priorityType', 'sectorType', 'createdBy', 'lastModifiedBy', 'property'],
relations: ['workOrderType', 'priorityType', 'sector', 'createdBy',
'lastModifiedBy', 'property'],
select: {
id: true,
property: {
id: true
id: true,
},
sectorType: {
sector: {
id: true,
type: true
type: true,
kind: true,
},
workOrderType: {
id: true,
Expand All @@ -74,31 +76,32 @@ const WorkOrderFields : FindOptions<WorkOrder> = {
serviceNeeded: true,
priorityType: {
id: true,
type: true
type: true,
},
description: true,
dueDate: true,
createdDate: true,
createdBy: {
id: true
id: true,
},
lastModifiedDate: true,
lastModifiedBy: {
id: true
id: true,
},
dateCompleted: true,
priceEstimate: true,
actualCost: true
}
}
actualCost: true,
},
};

const WorkOrderFieldsNoProperty : FindOptions<WorkOrder> = {
relations: ['workOrderType', 'priorityType', 'sectorType', 'createdBy', 'lastModifiedBy'],
relations: ['workOrderType', 'priorityType', 'sector', 'createdBy', 'lastModifiedBy'],
select: {
id: true,
sectorType: {
sector: {
id: true,
type: true
type: true,
kind: true,
},
workOrderType: {
id: true,
Expand All @@ -109,22 +112,23 @@ const WorkOrderFieldsNoProperty : FindOptions<WorkOrder> = {
serviceNeeded: true,
priorityType: {
id: true,
type: true
type: true,
},
description: true,
dueDate: true,
createdDate: true,
createdBy: {
id: true
id: true,
},
lastModifiedDate: true,
lastModifiedBy: {
id: true
id: true,
},
dateCompleted: true,
priceEstimate: true,
actualCost: true
}
}
actualCost: true,
},
};

export { PropertyFields, UserFields, WorkOrderFields, PropertyFieldsNoUser, WorkOrderFieldsNoProperty };
export { PropertyFields, UserFields, WorkOrderFields, PropertyFieldsNoUser,
WorkOrderFieldsNoProperty };
32 changes: 32 additions & 0 deletions backend/src/controllers/PropertySectorsController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import express, { Request, Response } from 'express';
import { PropertySectorService } from '../services/PropertySectorService';
import { PropertySectorMapper } from '../entity_mappers/PropertySectorMapper';
import auth from '../middleware/auth';
import handleError from '../utils/HttpUtils';
import validateBody from '../middleware/requestValidation';
import { PropertySectorDTO } from '../dtos/PropertySectorDTO';
import { PropertySector } from '../entities/PropertySector';
import { PROPERTY_SECTOR_FIELDS } from '../constants/BodyFields';

const propertySectorService = new PropertySectorService();
const propertySectorMapper = new PropertySectorMapper();

const propertySectorsController = express.Router({ mergeParams: true });

propertySectorsController.post('/', validateBody(PROPERTY_SECTOR_FIELDS.createFields), auth,
async (req: Request, res: Response) => {
try {
const propertySectorDTOs : PropertySectorDTO[] = req.body as PropertySectorDTO[];
const propertySectors : PropertySector[] = await propertySectorService
.createPropertySectors(
Number(req.params.propertyId),
propertySectorDTOs.map(propertySectorDTO =>
propertySectorMapper.fromDTO(propertySectorDTO)));
return res.status(200).json(propertySectors.map(propertySector =>
propertySectorMapper.toDTO(propertySector)));
} catch (err) {
return handleError(err, res);
}
});

export { propertySectorsController };
4 changes: 3 additions & 1 deletion backend/src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { userPropertiesController } from './UserPropertiesController';
import { propertyController } from './PropertyController';
import { propertyWorkOrdersController } from './PropertyWorkOrdersController';
import { workOrderController } from './WorkOrderController';
import { propertySectorsController } from './PropertySectorsController';

class Router {

Expand All @@ -14,7 +15,8 @@ class Router {
this.router.use('/api/users/:userId/properties', userPropertiesController)
this.router.use('/api/properties', propertyController);
this.router.use('/api/workOrders', workOrderController);
this.router.use('/api/properties/:propertyId/workorders', propertyWorkOrdersController);
this.router.use('/api/properties/:propertyId/workOrders', propertyWorkOrdersController);
this.router.use('/api/properties/:propertyId/sectors', propertySectorsController);
}

getRouter() {
Expand Down
10 changes: 10 additions & 0 deletions backend/src/dtos/PropertySectorDTO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { PropertyDTO } from './PropertyDTO';
import { SectorDTO } from './SectorDTO';

export class PropertySectorDTO {

id: number;
property: PropertyDTO;
sector: SectorDTO;
sectorKind: string;
}
12 changes: 12 additions & 0 deletions backend/src/dtos/SectorDTO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export class SectorDTO {

id: number;
type: string;
kind: string;

constructor(kind? : string) {
if (kind) {
this.kind = kind;
}
}
}
11 changes: 0 additions & 11 deletions backend/src/dtos/SectorTypeDTO.ts

This file was deleted.

15 changes: 8 additions & 7 deletions backend/src/dtos/WorkOrderDTO.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { PropertyDTO } from "./PropertyDTO";
import { SectorTypeDTO } from "./SectorTypeDTO";
import { WorkOrderTypeDTO } from "./WorkOrderTypeDTO";
import { PriorityTypeDTO } from "./PriorityTypeDTO";
import { UserDTO } from "./UserDTO";
import { PropertyDTO } from './PropertyDTO';
import { SectorDTO } from './SectorDTO';
import { WorkOrderTypeDTO } from './WorkOrderTypeDTO';
import { PriorityTypeDTO } from './PriorityTypeDTO';
import { UserDTO } from './UserDTO';

export class WorkOrderDTO {

id: number;
property: PropertyDTO;
sectorType: SectorTypeDTO | string;
sector: SectorDTO;
sectorType: string;
sectorKind: string;
workOrderType: WorkOrderTypeDTO | string;
title: string;
cause: string;
Expand Down
22 changes: 22 additions & 0 deletions backend/src/entities/PropertySector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Entity, PrimaryGeneratedColumn, ManyToOne, JoinColumn } from 'typeorm';
import { Property } from './Property';
import { Sector } from './Sector';

@Entity({ name: 'property_sectors' })
export class PropertySector {

@PrimaryGeneratedColumn()
id: number;

@ManyToOne(type => Property)
@JoinColumn({
name: 'property_id',
})
property: Property;

@ManyToOne(type => Sector)
@JoinColumn({
name: 'sector_id',
})
sector: Sector;
}
13 changes: 13 additions & 0 deletions backend/src/entities/Sector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

@Entity({ name: 'sectors' })
export class Sector {
@PrimaryGeneratedColumn()
id: number;

@Column()
type: string;

@Column()
kind: string;
}
11 changes: 0 additions & 11 deletions backend/src/entities/SectorType.ts

This file was deleted.

Loading

0 comments on commit 87b6e92

Please sign in to comment.