Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#159 #161 Refactoring sectors to include type & kind, Adding property address fields & refactoring #162

Merged
merged 7 commits into from
Dec 1, 2019
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
5 changes: 5 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"jsonwebtoken": "^8.5.1",
"mysql": "^2.17.1",
"password-validator": "^5.0.2",
"postcode-validator": "^3.0.0",
"reflect-metadata": "^0.1.13",
"validator": "^12.0.0"
},
Expand Down
22 changes: 13 additions & 9 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']
}
const PROPERTY_FIELDS = {
createFields : ['name', 'propertyType', 'address', 'city', 'province', 'postalCode', 'countryCode'],
};

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

export { UserFields, WorkOrderFields, PROPERTY_FIELDS, PROPERTY_SECTOR_FIELDS };
120 changes: 68 additions & 52 deletions backend/src/constants/FindOptionsFields.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,73 @@
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';
import { PropertySector } from '../entities/PropertySector';

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,
},
phoneNumber: 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 +78,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 +114,33 @@ 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,
},
};

const PROPERTY_SECTOR_FIELDS : FindOptions<PropertySector> = {
relations: ['sector'],
select: {
id: true,
sector: {
id: true,
},
},
};

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

const propertyService = new PropertyService();
const propertyMapper = new PropertyMapper();
const propertySectorService = new PropertySectorService();
const propertySectorMapper = new PropertySectorMapper();

const propertyController = express.Router();

Expand All @@ -15,16 +23,34 @@ propertyController.get('/:id', auth, async(req: Request, res: Response) => {
return res.status(200).json(propertyMapper.toDTO(property));
} catch (err) {
return handleError(err, res);
}
})
}
});

propertyController.patch('/:id', auth, async(req: Request, res: Response) => {
try {
await propertyService.updatePropertyById(Number(req.params.id), req.body);
return res.status(204).end();
} catch (err) {
return handleError(err, res);
}
})
}
});

propertyController.post(
'/:propertyId/sectors', validateArrayBody(PROPERTY_SECTOR_FIELDS.createFields), auth,
async (req: Request, res: Response) => {
try {
const { propertyId } = req.params;
const propertySectorDTOs : PropertySectorDTO[] = req.body as PropertySectorDTO[];
const propertySectors : PropertySector[] = await propertySectorService
.createPropertySectors(
Number(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 { propertyController };
26 changes: 26 additions & 0 deletions backend/src/controllers/PropertySectorsController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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 { PropertySectorDTO } from '../dtos/PropertySectorDTO';

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

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

propertySectorsController.patch('/:propertySectorId', auth, async (req: Request, res: Response) => {
try {
const { propertySectorId } = req.params;
const propertySectorDTO : PropertySectorDTO = req.body as PropertySectorDTO;
await propertySectorService.update(
Number(propertySectorId),
propertySectorMapper.fromDTO(propertySectorDTO));
return res.status(204).end();
} catch (err) {
return handleError(err, res);
}
})

export { propertySectorsController };
4 changes: 2 additions & 2 deletions backend/src/controllers/PropertyWorkOrdersController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { WorkOrderService } from '../services/WorkOrderService';
import { WorkOrderMapper } from '../entity_mappers/WorkOrderMapper';
import { WorkOrderDTO } from '../dtos/WorkOrderDTO';
import auth from '../middleware/auth';
import handleError from '../utils/HttpUtils';
import validateBody from '../middleware/requestValidation';
import { handleError } from '../utils/HttpUtils';
import { validateBody } from '../middleware/requestValidation';
import { WorkOrderFields } from '../constants/BodyFields';

const propertyWorkOrdersController = express.Router({mergeParams: true});
Expand Down
4 changes: 2 additions & 2 deletions backend/src/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import express, {Request, Response} from 'express';
import { UserService } from '../services/UserService';
import { UserMapper } from '../entity_mappers/UserMapper';
import auth from '../middleware/auth';
import handleError from '../utils/HttpUtils';
import validateBody from '../middleware/requestValidation';
import { handleError } from '../utils/HttpUtils';
import { validateBody } from '../middleware/requestValidation';
import { UserFields } from '../constants/BodyFields';
import { UserDTO } from 'src/dtos/UserDTO';

Expand Down
Loading