Skip to content

Commit

Permalink
Merge pull request #57 from icefoganalytics/refactor/improve-readabil…
Browse files Browse the repository at this point in the history
…ity-of-month-worksheet-code

Refactor: Improve Readability of Month Worksheet Code
  • Loading branch information
klondikemarlen authored Dec 8, 2023
2 parents 17794c9 + 18f6f0d commit 48ec2c6
Show file tree
Hide file tree
Showing 21 changed files with 707 additions and 358 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ All commands are just strings joined together, so it's easy to add new commmands

> NOTE: make sure you delete the .env file before runing a development setup again as it is auto-loaded by docker compose.
> NOTE: You can also skip seeding when database is not empty by setting the `SKIP_SEEDING_UNLESS_EMPTY=true` environment variable.
### Editor Setup

Your text editor or IDE might require you to manually install the dependencies to get TypesScript autocompletion working. Hopefully, it "just works :tm:". If not you can install packages locally like so:
Expand Down
18 changes: 17 additions & 1 deletion api/src/controllers/funding-submission-line-jsons-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ export class FundingSubmissionLineJsonsController extends BaseController {
})
}

async show() {
const fundingSubmissionLineJson = await this.loadFundingSubmissionLineJson()
if (isNil(fundingSubmissionLineJson))
return this.response.status(404).json({ message: "FundingSubmissionLineJson not found." })

const serializedfundingSubmissionLineJson =
FundingSubmissionLineJsonSerializer.asDetailed(fundingSubmissionLineJson)
return this.response.json({
fundingSubmissionLineJson: serializedfundingSubmissionLineJson,
})
}

async create() {
return FundingSubmissionLineJson.create(this.request.body)
.then((fundingSubmissionLineJson) => {
Expand All @@ -48,7 +60,11 @@ export class FundingSubmissionLineJsonsController extends BaseController {
return fundingSubmissionLineJson
.update(this.request.body)
.then((fundingSubmissionLineJson) => {
return this.response.json({ fundingSubmissionLineJson })
const serializedfundingSubmissionLineJson =
FundingSubmissionLineJsonSerializer.asDetailed(fundingSubmissionLineJson)
return this.response.json({
fundingSubmissionLineJson: serializedfundingSubmissionLineJson,
})
})
.catch((error) => {
return this.response
Expand Down
9 changes: 9 additions & 0 deletions api/src/initializers/30-run-seeds.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { seeder } from "@/db/umzug"
import { Centre } from "@/models"

export async function runSeeds(): Promise<void> {
if (process.env.SKIP_SEEDING_UNLESS_EMPTY === "true") {
const count = await Centre.count()
if (count > 0) {
console.log("Skipping seeding as SKIP_SEEDING_UNLESS_EMPTY set, and data already seeded.")
return
}
}

await seeder.up()
return
}
Expand Down
15 changes: 11 additions & 4 deletions api/src/models/funding-submission-line-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class FundingSubmissionLineJson extends Model<
declare dateStart: Date
declare dateEnd: Date
declare values: string
declare lines: CreationOptional<FundingLineValue[]>
declare createdAt: CreationOptional<Date>
declare updatedAt: CreationOptional<Date>

Expand All @@ -50,10 +51,6 @@ export class FundingSubmissionLineJson extends Model<
foreignKey: "centreId",
})
}

get lines(): NonAttribute<FundingLineValue[]> {
return JSON.parse(this.values)
}
}

FundingSubmissionLineJson.init(
Expand Down Expand Up @@ -92,6 +89,16 @@ FundingSubmissionLineJson.init(
type: DataTypes.TEXT,
allowNull: false,
},
// TODO: investigate moving these to a get/set on the "values" attribute
lines: {
type: DataTypes.VIRTUAL,
get() {
return JSON.parse(this.getDataValue("values"))
},
set(value: FundingLineValue[]) {
this.setDataValue("values", JSON.stringify(value))
},
},
createdAt: {
type: DataTypes.DATE,
allowNull: false,
Expand Down
4 changes: 4 additions & 0 deletions api/src/routes/api-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ apiRouter.delete("/api/payments/:paymentId", PaymentsController.destroy)

apiRouter.get("/api/funding-submission-line-jsons", FundingSubmissionLineJsonsController.index)
apiRouter.post("/api/funding-submission-line-jsons", FundingSubmissionLineJsonsController.create)
apiRouter.get(
"/api/funding-submission-line-jsons/:fundingSubmissionLineJsonId",
FundingSubmissionLineJsonsController.show
)
apiRouter.patch(
"/api/funding-submission-line-jsons/:fundingSubmissionLineJsonId",
FundingSubmissionLineJsonsController.update
Expand Down
5 changes: 4 additions & 1 deletion api/src/routes/centre-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ centreRouter.get("/:id/worksheets", async (req: Request, res: Response) => {
return res.status(404).json({ message: "Must provide a centre id" })
}

const worksheets = await FundingSubmissionLineJson.findAll({ where: { centreId: id } })
const worksheets = await FundingSubmissionLineJson.findAll({
where: { centreId: id },
order: ["dateStart"],
})
const serializedGroups = FundingSubmissionLineJsonSerializer.serializeWorksheetsView(worksheets)
return res.json({ data: serializedGroups })
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sortBy, uniq, pick } from "lodash"
import { sortBy, uniq, pick, omit } from "lodash"
import moment from "moment"
import { FundingSubmissionLineJson } from "@/models"

Expand All @@ -23,6 +23,13 @@ export class FundingSubmissionLineJsonSerializer extends BaseSerializer<FundingS
})
}

static asDetailed(fundingSubmissionLineJson: FundingSubmissionLineJson) {
return {
...omit(fundingSubmissionLineJson.dataValues, "values"),
lines: fundingSubmissionLineJson.lines,
}
}

constructor(modelOrModels: FundingSubmissionLineJson | FundingSubmissionLineJson[]) {
super(modelOrModels)
}
Expand Down
9 changes: 8 additions & 1 deletion api/src/services/funding-submission-line-json-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class FundingSubmissionLineJsonServices implements BaseService {
static async bulkCreate(centreId: number, fiscalYear: string) {
const worksheets = await FundingSubmissionLineJson.findAll({
where: { centreId, fiscalYear },
order: ["dateStart"],
})
if (worksheets.length > 0) {
throw new Error("Fiscal year already exists for this centre")
Expand Down Expand Up @@ -52,7 +53,13 @@ export class FundingSubmissionLineJsonServices implements BaseService {
date = date.add(1, "month")
}

return FundingSubmissionLineJson.bulkCreate(bulkAttributes)
await FundingSubmissionLineJson.bulkCreate(bulkAttributes)

// Ensure consistent ordering, as bulk create does not guarantee order
return FundingSubmissionLineJson.findAll({
where: { fiscalYear },
order: ["dateStart"],
})
}
}

Expand Down
8 changes: 8 additions & 0 deletions bin/dev
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ class DevHelper
exit 0
end

def web(*args, **kwargs)
run(*%w[web], *args, **kwargs)
end

def api(*args, **kwargs)
run(*%w[web], *args, **kwargs)
end

def npm(*args, **kwargs)
run(*%w[api npm], *args, **kwargs)
end
Expand Down
18 changes: 9 additions & 9 deletions web/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 web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"qs": "^6.11.2",
"vue": "^3.2.38",
"vue-currency-input": "^3.0.5",
"vue-router": "^4.1.6",
"vue-router": "^4.2.5",
"vue3-apexcharts": "^1.4.1",
"vuetify": "^3.4.0"
},
Expand Down
16 changes: 16 additions & 0 deletions web/src/api/centres-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import http from "@/api/http-client"

import { FundingSubmissionLineJson } from "./funding-submission-line-jsons-api"

export const centresApi = {
fiscalYear: {
// TODO: normalize this route return type and format
create(centerId: number, fiscalYear: string): Promise<FundingSubmissionLineJson[]> {
return http
.post(`/api/centre/${centerId}/fiscal-year`, { fiscalYear })
.then(({ data }) => data.data)
},
},
}

export default centresApi
5 changes: 5 additions & 0 deletions web/src/api/funding-submission-line-jsons-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export const fundingSubmissionLineJsonsApi = {
}> {
return http.get("/api/funding-submission-line-jsons", { params }).then(({ data }) => data)
},
get(fundingSubmissionLineJsonId: number) {
return http
.get(`/api/funding-submission-line-jsons/${fundingSubmissionLineJsonId}`)
.then(({ data }) => data)
},
create(
attributes: Partial<FundingSubmissionLineJson>
): Promise<{ fundingSubmissionLineJson: FundingSubmissionLineJson }> {
Expand Down
Loading

0 comments on commit 48ec2c6

Please sign in to comment.