Skip to content

Commit 8a92789

Browse files
henrikhenrikperssonHenrik Persson
andauthored
MIM-380: Handle more work orders (#254)
* Update onecore-types dependency * Add HearingImpaired to CreateWorkOrderDetailsFactory * Handle more work orders * Fix test name --------- Co-authored-by: Henrik Persson <henrik.persson@iteam.com>
1 parent b3a1cda commit 8a92789

File tree

5 files changed

+39
-48
lines changed

5 files changed

+39
-48
lines changed

package-lock.json

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"koa2-swagger-ui": "^5.10.0",
7070
"lodash": "^4.17.21",
7171
"odoo-await": "^3.4.1",
72-
"onecore-types": "^2.6.1",
72+
"onecore-types": "^3.4.0",
7373
"onecore-utilities": "^1.1.0",
7474
"pino": "^9.1.0",
7575
"pino-elasticsearch": "^8.0.0",

src/services/work-order-service/index.ts

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -394,17 +394,17 @@ export const routes = (router: KoaRouter) => {
394394
Images,
395395
} = ctx.request.body
396396

397-
// Filter out workOrders that are not related to laundry rooms
398-
const laundryRoomWorkOrderRequests = Rows.filter(
399-
(workOrder: any) => workOrder.LocationCode === 'TV'
397+
// Filter out workOrders that are not handled by onecore
398+
const onecoreWorkOrderRequests = Rows.filter((workOrder: any) =>
399+
['TV', 'BWC', 'KÖ'].includes(workOrder.LocationCode)
400400
)
401401

402402
const reason = !ContactCode
403403
? 'ContactCode is missing'
404404
: !RentalObjectCode
405405
? 'RentalObjectCode is missing'
406-
: laundryRoomWorkOrderRequests.length === 0
407-
? 'No work orders on laundry rooms found in request'
406+
: onecoreWorkOrderRequests.length === 0
407+
? 'No supported work orders found in request'
408408
: null
409409

410410
if (reason) {
@@ -428,20 +428,6 @@ export const routes = (router: KoaRouter) => {
428428
return
429429
}
430430

431-
// Check if rental property has laundry room access
432-
const propertyHasLaundryRoomAccess =
433-
rentalPropertyInfo.maintenanceUnits?.find(
434-
(unit) => unit.type.toUpperCase() === 'TVÄTTSTUGA'
435-
)
436-
if (!propertyHasLaundryRoomAccess) {
437-
ctx.status = 404
438-
ctx.body = {
439-
reason: 'No laundry room found for rental property',
440-
...metadata,
441-
}
442-
return
443-
}
444-
445431
// Get tenant with leases by contact code
446432
const tenant = await leasingAdapter.getTenantByContactCode(ContactCode)
447433
if (!tenant.ok) {
@@ -466,34 +452,39 @@ export const routes = (router: KoaRouter) => {
466452
return
467453
}
468454

469-
for (let workOrderRequest of laundryRoomWorkOrderRequests) {
470-
workOrderRequest = {
471-
rentalPropertyInfo: rentalPropertyInfo,
472-
tenant: tenant.data,
473-
lease: lease,
474-
details: {
475-
ContactCode,
476-
RentalObjectCode,
477-
AccessOptions,
478-
HearingImpaired,
479-
Pet,
480-
Images,
481-
Rows: [workOrderRequest],
482-
},
483-
}
484-
workOrderAdapter.createWorkOrder(workOrderRequest)
485-
}
455+
const results = await Promise.all(
456+
onecoreWorkOrderRequests.map((workOrderRequest: any) =>
457+
workOrderAdapter.createWorkOrder({
458+
rentalPropertyInfo: rentalPropertyInfo,
459+
tenant: tenant.data,
460+
lease: lease,
461+
details: {
462+
ContactCode,
463+
RentalObjectCode,
464+
AccessOptions,
465+
HearingImpaired,
466+
Pet,
467+
Images,
468+
Rows: [workOrderRequest],
469+
},
470+
})
471+
)
472+
)
486473

487474
ctx.status = 200
488475
ctx.body = {
489-
message: `Work order created`,
476+
message: `Work orders created`,
477+
// TODO better handling/response when there is an error with creating one or more work orders in the batch
478+
errors: results
479+
.filter((result) => !result.ok)
480+
.map((result) => result.err),
490481
...metadata,
491482
}
492483
} catch (error) {
493-
logger.error(error, 'Error creating new work order')
484+
logger.error(error, 'Error creating new work orders')
494485
ctx.status = 500
495486
ctx.body = {
496-
error: 'Failed to create a new work order',
487+
error: 'Failed to create new work orders',
497488
...metadata,
498489
}
499490
}

src/services/work-order-service/tests/index.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,14 @@ describe('work-order-service index', () => {
252252
expect(res.body.reason).toBe('RentalObjectCode is missing')
253253
})
254254

255-
it('should return 400 if no work orders on laundry rooms found in request', async () => {
255+
it('should return 400 if no supported work orders found in request', async () => {
256256
createWorkOrderDetailsMock.Rows = []
257257
const res = await request(app.callback())
258258
.post('/api/workOrders')
259259
.send(createWorkOrderDetailsMock)
260260

261261
expect(res.status).toBe(400)
262-
expect(res.body.reason).toBe(
263-
'No work orders on laundry rooms found in request'
264-
)
262+
expect(res.body.reason).toBe('No supported work orders found in request')
265263
})
266264

267265
it('should return 404 if rental property not found', async () => {

test/factories/work-order.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const CreateWorkOrderDetailsFactory =
5252
CallBetween: '08:00 - 17:00',
5353
},
5454
Pet: false,
55+
HearingImpaired: false,
5556
Rows: [
5657
{
5758
LocationCode: 'TV',

0 commit comments

Comments
 (0)