Skip to content

Commit ec4dc21

Browse files
mittistormensandnuggahgithub-actions[bot]0x4dsvjson
authored
Develop uthyrning bostader (#95)
* Feature/uth 203 populera kundkortboendereferens formular med data fran api (#94) --------- Co-authored-by: sandnuggah <adam@fapfap.se> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: 0x4d <0x4d@undefined> Co-authored-by: Sven Johansson <sven.johansson@iteam.se>
1 parent a6cc99b commit ec4dc21

33 files changed

+683
-304
lines changed

.vscode/extensions.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

.vscode/launch.json

Lines changed: 0 additions & 27 deletions
This file was deleted.

.vscode/settings.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

.vscode/tasks.json

Lines changed: 0 additions & 46 deletions
This file was deleted.

packages/backend/.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v18.*
1+
v20.17.0

packages/backend/.tool-versions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nodejs 18.20.5
1+
nodejs 23.5.0

packages/backend/package-lock.json

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

packages/backend/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"koa-pino-logger": "^4.0.0",
5353
"koa-session": "^6.4.0",
5454
"onecore-types": "^3.5.2",
55-
"onecore-utilities": "^1.1.0"
55+
"onecore-utilities": "^1.1.0",
56+
"zod": "^3.24.2"
5657
}
5758
}

packages/backend/src/services/leasing-service/adapters/core-adapter.ts

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import {
1010
OfferWithOfferApplicants,
1111
ReplyToOfferErrorCodes,
1212
Tenant,
13+
leasing,
14+
schemas,
1315
} from 'onecore-types'
1416
import { AxiosError, HttpStatusCode } from 'axios'
15-
17+
import { z } from 'zod'
1618
import Config from '../../../common/config'
1719
import { getFromCore } from '../../common/adapters/core-adapter'
1820

@@ -144,6 +146,21 @@ const getTenantByContactCode = async (
144146
}
145147
}
146148

149+
const getContactByContactCode = async (
150+
contactCode: string
151+
): Promise<AdapterResult<Contact, unknown>> => {
152+
try {
153+
const result = await getFromCore<{ content: Contact }>({
154+
method: 'get',
155+
url: `${coreBaseUrl}/contact/contactCode/${contactCode}`,
156+
}).then((res) => res.data)
157+
158+
return { ok: true, data: result.content }
159+
} catch (err) {
160+
return { ok: false, err, statusCode: 500 }
161+
}
162+
}
163+
147164
const validatePropertyRentalRules = async (
148165
contactCode: string,
149166
rentalObjectCode: string
@@ -403,12 +420,88 @@ const getActiveOfferByListingId = async (
403420
}
404421
}
405422

423+
type ApplicationProfile = z.infer<typeof schemas.v1.ApplicationProfileSchema>
424+
425+
enum GetApplicationProfileByContactCodeErrorCodes {
426+
NotFound = 'not-found',
427+
Unknown = 'unknown',
428+
}
429+
430+
const getApplicationProfileByContactCode = async (
431+
contactCode: string
432+
): Promise<AdapterResult<ApplicationProfile, unknown>> => {
433+
try {
434+
const {
435+
data: { content: applicationProfile },
436+
} = await getFromCore<{
437+
content: ApplicationProfile
438+
}>({
439+
method: 'get',
440+
url: `${coreBaseUrl}/contacts/${contactCode}/application-profile`,
441+
})
442+
443+
return {
444+
ok: true,
445+
data: applicationProfile,
446+
}
447+
} catch (error) {
448+
if (!(error instanceof AxiosError)) {
449+
return {
450+
ok: false,
451+
err: GetApplicationProfileByContactCodeErrorCodes.Unknown,
452+
statusCode: 500,
453+
}
454+
}
455+
if (error.response?.status === 404) {
456+
return {
457+
ok: false,
458+
err: GetApplicationProfileByContactCodeErrorCodes.NotFound,
459+
statusCode: 404,
460+
}
461+
} else {
462+
return {
463+
ok: false,
464+
err: GetApplicationProfileByContactCodeErrorCodes.Unknown,
465+
statusCode: 500,
466+
}
467+
}
468+
}
469+
}
470+
471+
type CreateOrUpdateApplicationProfileRequestParams = z.infer<
472+
typeof leasing.v1.CreateOrUpdateApplicationProfileRequestParamsSchema
473+
>
474+
475+
type CreateOrUpdateApplicationProfileResponseData = z.infer<
476+
typeof leasing.v1.CreateOrUpdateApplicationProfileResponseDataSchema
477+
>
478+
479+
const createOrUpdateApplicationProfile = async (
480+
contactCode: string,
481+
profileCommand: CreateOrUpdateApplicationProfileRequestParams
482+
): Promise<
483+
AdapterResult<CreateOrUpdateApplicationProfileResponseData, unknown>
484+
> => {
485+
try {
486+
const response = await getFromCore<any>({
487+
method: 'post',
488+
url: `${coreBaseUrl}/contacts/${contactCode}/application-profile/admin`,
489+
data: profileCommand,
490+
})
491+
492+
return { ok: true, data: response.data.content }
493+
} catch (err) {
494+
return { ok: false, err: 'unknown', statusCode: 500 }
495+
}
496+
}
497+
406498
export {
407499
getListingsWithApplicants,
408500
getListingWithApplicants,
409501
removeApplicant,
410502
getContactsDataBySearchQuery,
411503
getTenantByContactCode,
504+
getContactByContactCode,
412505
createNoteOfInterestForInternalParkingSpace,
413506
validatePropertyRentalRules,
414507
validateResidentialAreaRentalRules,
@@ -419,4 +512,6 @@ export {
419512
acceptOffer,
420513
denyOffer,
421514
getActiveOfferByListingId,
515+
getApplicationProfileByContactCode,
516+
createOrUpdateApplicationProfile,
422517
}

packages/backend/src/services/leasing-service/index.ts

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const routes = (router: KoaRouter) => {
6565

6666
router.get('(.*)/contact/:contactCode', async (ctx) => {
6767
const metadata = generateRouteMetadata(ctx)
68-
const result = await coreAdapter.getTenantByContactCode(
68+
const result = await coreAdapter.getContactByContactCode(
6969
ctx.params.contactCode
7070
)
7171

@@ -311,4 +311,68 @@ export const routes = (router: KoaRouter) => {
311311
ctx.body = { error: result.err, ...metadata }
312312
}
313313
})
314+
315+
router.get('(.*)/contacts/:contactCode/application-profile', async (ctx) => {
316+
const metadata = generateRouteMetadata(ctx, ['q'])
317+
318+
const [applicationProfileResult, contactResult] = await Promise.all([
319+
coreAdapter.getApplicationProfileByContactCode(ctx.params.contactCode),
320+
coreAdapter.getContactByContactCode(ctx.params.contactCode),
321+
])
322+
323+
let applicationProfile = null
324+
325+
// FIXME: Should status 404 not be allowed to be "ok" ?
326+
if (!applicationProfileResult.ok) {
327+
if (applicationProfileResult.statusCode != 404) {
328+
ctx.status = applicationProfileResult.statusCode
329+
ctx.body = { error: applicationProfileResult.err, ...metadata }
330+
return
331+
}
332+
} else {
333+
applicationProfile = applicationProfileResult.data
334+
}
335+
336+
if (!contactResult.ok) {
337+
ctx.status = contactResult.statusCode
338+
ctx.body = { error: contactResult.err, ...metadata }
339+
return
340+
}
341+
342+
ctx.status = 200
343+
ctx.body = {
344+
content: {
345+
applicationProfile,
346+
contact: contactResult.data,
347+
},
348+
...metadata,
349+
}
350+
})
351+
352+
router.put('(.*)/contacts/:contactCode/application-profile', async (ctx) => {
353+
const metadata = generateRouteMetadata(ctx, ['q'])
354+
355+
const result = await coreAdapter.createOrUpdateApplicationProfile(
356+
ctx.params.contactCode,
357+
{
358+
...ctx.request.body,
359+
housingReference: {
360+
...ctx.request.body.housingReference,
361+
reviewedBy: ctx.session?.account.name,
362+
},
363+
}
364+
)
365+
366+
if (!result.ok) {
367+
ctx.status = result.statusCode
368+
ctx.body = { error: result.err, ...metadata }
369+
return
370+
}
371+
372+
ctx.status = 200
373+
ctx.body = {
374+
content: result.data,
375+
...metadata,
376+
}
377+
})
314378
}

0 commit comments

Comments
 (0)