Skip to content

Commit

Permalink
Merge pull request #62 from Bostads-AB-Mimer/feat/1230-update-status-…
Browse files Browse the repository at this point in the history
…on-expired-listings

feat: script to update expired listings
  • Loading branch information
schweinryder authored May 22, 2024
2 parents c19cb00 + f5dc964 commit 86914cb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"migrate:up": "knex migrate:latest --env dev",
"migrate:down": "knex migrate:down --env dev",
"seed": "knex seed:run --env dev",
"script:expire-listings": "ts-node src/scripts/expire-listings.ts",
"ts:watch": "tsc --watch --noEmit"
},
"author": "",
Expand Down Expand Up @@ -59,7 +60,7 @@
"koa-bodyparser": "^4.4.1",
"koa-pino-logger": "^4.0.0",
"mssql": "^9.3.2",
"onecore-types": "^1.18.0",
"onecore-types": "^1.19.0",
"personnummer": "^3.2.1",
"prettier": "^3.2.5",
"randexp": "^0.5.3",
Expand Down
18 changes: 18 additions & 0 deletions src/scripts/expire-listings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-disable no-process-exit */
import { getExpiredListings, updateListingStatuses } from '../services/lease-service/adapters/listing-adapter'
import { ListingStatus } from 'onecore-types'

async function updateExpiredListings() {
const expiredListings = await getExpiredListings();
console.log('Expired listings: ', expiredListings);
if (expiredListings.length > 0) {
const expiredListingsIds = expiredListings.map(l => l.Id);
console.log(`Found ${expiredListingsIds} expired listings`);
const updateCount = await updateListingStatuses(expiredListingsIds, ListingStatus.Expired);
console.log(`Updated ${updateCount} expired listings`);
}
console.log('Expired listings updated successfully')
process.exit(0);
}

updateExpiredListings();
20 changes: 19 additions & 1 deletion src/services/lease-service/adapters/listing-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Applicant, Listing, ApplicantStatus } from 'onecore-types'
import { Applicant, Listing, ApplicantStatus, ListingStatus } from 'onecore-types'
import { db } from './db'

function transformFromDbListing(row: any): Listing {
Expand Down Expand Up @@ -275,6 +275,22 @@ const applicationExists = async (contactCode: string, listingId: number) => {
return !!result // Convert result to boolean: true if exists, false if not
}

const getExpiredListings = async () => {
const currentDate = new Date()
const listings = await db('listing')
.where('PublishedTo', '<', currentDate)
.andWhere('Status', '==', ListingStatus.Active)
return listings
}

const updateListingStatuses = async (listingIds: number[], status: ListingStatus) => {
const updateCount = await db('listing')
.whereIn('Id', listingIds)
.update({ Status: status })

return updateCount
}

export {
createListing,
createApplication,
Expand All @@ -286,4 +302,6 @@ export {
getApplicantsByContactCodeAndRentalObjectCode,
applicationExists,
updateApplicantStatus,
getExpiredListings,
updateListingStatuses
}

0 comments on commit 86914cb

Please sign in to comment.