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
9 changes: 5 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"koa2-swagger-ui": "^5.10.0",
"lodash": "^4.17.21",
"odoo-await": "^3.4.1",
"onecore-types": "^2.6.1",
"onecore-types": "^3.4.0",
"onecore-utilities": "^1.1.0",
"pino": "^9.1.0",
"pino-elasticsearch": "^8.0.0",
Expand Down
69 changes: 30 additions & 39 deletions src/services/work-order-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,17 +394,17 @@ export const routes = (router: KoaRouter) => {
Images,
} = ctx.request.body

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Det här känns ju egentligen inte nödvändigt, men kanske bra att ha?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nej sant, kanske värt att ta bort?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ja!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jag fixar det i #255

)

const reason = !ContactCode
? 'ContactCode is missing'
: !RentalObjectCode
? 'RentalObjectCode is missing'
: laundryRoomWorkOrderRequests.length === 0
? 'No work orders on laundry rooms found in request'
: onecoreWorkOrderRequests.length === 0
? 'No supported work orders found in request'
: null

if (reason) {
Expand All @@ -428,20 +428,6 @@ export const routes = (router: KoaRouter) => {
return
}

// Check if rental property has laundry room access
const propertyHasLaundryRoomAccess =
rentalPropertyInfo.maintenanceUnits?.find(
(unit) => unit.type.toUpperCase() === 'TVÄTTSTUGA'
)
if (!propertyHasLaundryRoomAccess) {
ctx.status = 404
ctx.body = {
reason: 'No laundry room found for rental property',
...metadata,
}
return
}

// Get tenant with leases by contact code
const tenant = await leasingAdapter.getTenantByContactCode(ContactCode)
if (!tenant.ok) {
Expand All @@ -466,34 +452,39 @@ export const routes = (router: KoaRouter) => {
return
}

for (let workOrderRequest of laundryRoomWorkOrderRequests) {
workOrderRequest = {
rentalPropertyInfo: rentalPropertyInfo,
tenant: tenant.data,
lease: lease,
details: {
ContactCode,
RentalObjectCode,
AccessOptions,
HearingImpaired,
Pet,
Images,
Rows: [workOrderRequest],
},
}
workOrderAdapter.createWorkOrder(workOrderRequest)
}
const results = await Promise.all(
onecoreWorkOrderRequests.map((workOrderRequest: any) =>
workOrderAdapter.createWorkOrder({
rentalPropertyInfo: rentalPropertyInfo,
tenant: tenant.data,
lease: lease,
details: {
ContactCode,
RentalObjectCode,
AccessOptions,
HearingImpaired,
Pet,
Images,
Rows: [workOrderRequest],
},
})
)
)

ctx.status = 200
ctx.body = {
message: `Work order created`,
message: `Work orders created`,
// TODO better handling/response when there is an error with creating one or more work orders in the batch
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

errors: results
.filter((result) => !result.ok)
.map((result) => result.err),
...metadata,
}
} catch (error) {
logger.error(error, 'Error creating new work order')
logger.error(error, 'Error creating new work orders')
ctx.status = 500
ctx.body = {
error: 'Failed to create a new work order',
error: 'Failed to create new work orders',
...metadata,
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/services/work-order-service/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,14 @@ describe('work-order-service index', () => {
expect(res.body.reason).toBe('RentalObjectCode is missing')
})

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

expect(res.status).toBe(400)
expect(res.body.reason).toBe(
'No work orders on laundry rooms found in request'
)
expect(res.body.reason).toBe('No supported work orders found in request')
})

it('should return 404 if rental property not found', async () => {
Expand Down
1 change: 1 addition & 0 deletions test/factories/work-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const CreateWorkOrderDetailsFactory =
CallBetween: '08:00 - 17:00',
},
Pet: false,
HearingImpaired: false,
Rows: [
{
LocationCode: 'TV',
Expand Down
Loading