Skip to content

Commit

Permalink
Merge pull request #46 from Bostads-AB-Mimer/feat/1262-endpoints-for-…
Browse files Browse the repository at this point in the history
…mimer.nu

Feat 1262 Endpoints for mimer.nu
  • Loading branch information
mittistormen authored Apr 30, 2024
2 parents b11bf89 + 966a8da commit 6f5b090
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 31 deletions.
16 changes: 16 additions & 0 deletions src/services/lease-service/adapters/listing-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ const getListingById = async (
return transformListing(parseApplicantsJson(result))
}

const getApplicantById = async (
applicantId: number
): Promise<Applicant | undefined> => {
const applicant = await db('Applicant')
.where({
Id: applicantId,
})
.first()

if (applicant == undefined) {
return undefined
}
return transformDbApplicant(applicant)
}

const createApplication = async (applicationData: Applicant) => {
await db('applicant').insert({
Name: applicationData.name,
Expand Down Expand Up @@ -239,6 +254,7 @@ export {
getListingById,
getListingByRentalObjectCode,
getAllListingsWithApplicants,
getApplicantById,
getApplicantsByContactCode,
getApplicantsByContactCodeAndRentalObjectCode,
applicationExists,
Expand Down
69 changes: 38 additions & 31 deletions src/services/lease-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
createListing,
createApplication,
getAllListingsWithApplicants,
getApplicantById,
getApplicantsByContactCode,
getApplicantsByContactCodeAndRentalObjectCode as getApplicantByContactCodeAndRentalObjectCode,
getListingByRentalObjectCode,
Expand All @@ -37,7 +38,7 @@ import {
getInvoicesByContactCode,
getUnpaidInvoicesByContactCode,
} from './adapters/xpand/invoices-adapter'
import { Applicant, Listing } from 'onecore-types'
import { Applicant, ApplicantStatus, Listing } from 'onecore-types'

interface CreateLeaseRequest {
parkingSpaceId: string
Expand Down Expand Up @@ -302,21 +303,17 @@ export const routes = (router: KoaRouter) => {

router.get('/listings/by-id/:listingId', async (ctx) => {
try {
const listingId = ctx.params.listingId;
const listing = await getListingById(listingId);
if(listing == undefined){
ctx.status = 404;
const listingId = ctx.params.listingId
const listing = await getListingById(listingId)
if (listing == undefined) {
ctx.status = 404
return
}

ctx.body = listing;
ctx.status = 200;
ctx.body = listing
ctx.status = 200
} catch (error) {
console.error(
'Error fetching listing:',
ctx.params.listingId,
error
)
console.error('Error fetching listing:', ctx.params.listingId, error)
ctx.status = 500 // Internal Server Error
ctx.body = {
error:
Expand All @@ -328,15 +325,15 @@ export const routes = (router: KoaRouter) => {

router.get('/listings/by-code/:rentalObjectCode', async (ctx) => {
try {
const rentaLObjectCode = ctx.params.rentalObjectCode;
const listing = await getListingByRentalObjectCode(rentaLObjectCode);
if(listing == undefined){
ctx.status = 404;
const rentaLObjectCode = ctx.params.rentalObjectCode
const listing = await getListingByRentalObjectCode(rentaLObjectCode)
if (listing == undefined) {
ctx.status = 404
return
}

ctx.body = listing;
ctx.status = 200;
ctx.body = listing
ctx.status = 200
} catch (error) {
console.error(
'Error fetching listing:',
Expand All @@ -354,7 +351,6 @@ export const routes = (router: KoaRouter) => {

router.get('/listings-with-applicants', async (ctx) => {
try {
console.log('Fetching listings with applicants...')
const listingsWithApplicants = await getAllListingsWithApplicants()
ctx.body = listingsWithApplicants
ctx.status = 200
Expand Down Expand Up @@ -422,25 +418,36 @@ export const routes = (router: KoaRouter) => {
})

router.patch('/applicants/:id/status', async (ctx) => {
const { id } = ctx.params;
const status = ctx.request.body as any;
const { id } = ctx.params
const { status, contactCode } = ctx.request.body as any

try {
const applicantUpdated = await updateApplicantStatus(Number(id), status.status);
//if the applicant is withdrawn by the user, make sure the application belongs to that particular user
if (status == ApplicantStatus.WithdrawnByUser) {
const applicant = await getApplicantById(Number(id))
if (applicant?.contactCode != contactCode) {
ctx.status = 404
ctx.body = { error: 'Applicant not found for this contactCode' }
return
}
}

const applicantUpdated = await updateApplicantStatus(Number(id), status)
if (applicantUpdated) {
ctx.status = 200;
ctx.body = { message: 'Applicant status updated successfully' };
ctx.status = 200
ctx.body = { message: 'Applicant status updated successfully' }
} else {
ctx.status = 404;
ctx.body = { error: 'Applicant not found' };
ctx.status = 404
ctx.body = { error: 'Applicant not found' }
}
} catch (error) {
console.error('Error updating applicant status:', error);
ctx.status = 500; // Internal Server Error
ctx.body = { error: 'An error occurred while updating the applicant status.' };
console.error('Error updating applicant status:', error)
ctx.status = 500 // Internal Server Error
ctx.body = {
error: 'An error occurred while updating the applicant status.',
}
}
});

})

/**
* Gets the waiting lists of a person.
Expand Down

0 comments on commit 6f5b090

Please sign in to comment.