Skip to content
This repository has been archived by the owner on May 5, 2022. It is now read-only.

Commit

Permalink
Merge pull request #108 from echo-webkom/develop
Browse files Browse the repository at this point in the history
Registration count
  • Loading branch information
bakseter authored Aug 29, 2021
2 parents 9ce9a76 + 56de31e commit 976bba4
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 23 deletions.
36 changes: 36 additions & 0 deletions scripts/get_count_registrations
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

# Default values
host=http://localhost
port=8080
user=bedkom

while getopts zpe:s:x:uw flag; do
case "${flag}" in
z) host=${OPTARG} ;;
p) port=${OPTARG} ;;
s) slug=${OPTARG} ;;
x) secret=${OPTARG} ;;
u) user=${OPTARG} ;;
w) waitlist=${OPTARG} ;;
*) echo "Invalid flag." ;;
esac
done

if [ -z ${secret+x} ]; then
echo "No authorization header specified (use -x)."
exit 1
fi

if [ -z ${slug+x} ]; then
echo "No slug specified (use -s)."
exit 1
elif [ -z ${waitlist+x} ]; then
curl --verbose \
-u "$user":"$secret" \
"$host:$port/registration?count=y&slug=$slug"
else
curl --verbose \
-u "$user":"$secret" \
"$host:$port/registration?count=y&slug=$slug&waitlist=y"
fi
4 changes: 2 additions & 2 deletions scripts/submit_bedpres
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if [ "$testing" = true ]; then
-u "$user":"$secret" \
-H "Connection: close" \
-H "Content-Type: application/json" \
-d '{ "slug": "bedriftspresentasjon-med-bekk", "spots": 50, "registrationDate": "2021-05-06T16:46+01:00" }' \
-d '{ "slug": "bedriftspresentasjon-med-bekk", "spots": 50, "registrationDate": "2021-05-06T16:46+01:00", "minDegreeYear": 1, "maxDegreeYear": 5 }' \
"$host":"$port"/bedpres
exit 0
fi
Expand All @@ -56,5 +56,5 @@ curl --verbose \
-u "$user":"$secret" \
-H "Connection: close" \
-H "Content-Type: application/json" \
-d '{ "slug": "'"${slug}"'", "spots": '"${spots}"', "registrationDate": "'"${regdate}"'" }' \
-d '{ "slug": "'"${slug}"'", "spots": '"${spots}"', "registrationDate": "'"${regdate}"'", "minDegreeYear": 1, "maxDegreeYear": 5 }' \
"$host":"$port"/bedpres
14 changes: 10 additions & 4 deletions src/main/kotlin/no/uib/echo/Response.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,19 @@ private fun resToMsg(res: Response): Pair<String, String> {
Response.TooEarly ->
return Pair("Påmeldingen er ikke åpen enda.", "Vennligst vent.")
Response.WaitList ->
return Pair("Plassene er dessverre fylt opp...", "Du har blitt satt på venteliste.")
return Pair(
"Alle plassene er dessverre fylt opp...",
"Du har blitt satt på venteliste, og vil bli kontaktet om det åpner seg en ledig plass."
)
Response.BedpresDosntExist ->
return Pair("Denne bedpres'en finnes ikke.", "Om du mener dette ikke stemmer, ta kontakt med Webkom.")
return Pair(
"Denne bedriftspresentasjonen finnes ikke.",
"Om du mener dette ikke stemmer, ta kontakt med Webkom."
)
Response.NotInRange ->
return Pair("Du kan dessverre ikke melde deg på.", "")
Response.OK ->
return Pair("Påmeldingen din er registrert!", "")
return Pair("Påmeldingen din er registrert!", "Du har fått plass på bedriftspresentasjonen.")
}
}

Expand All @@ -53,7 +59,7 @@ fun resToJson(res: Response, date: String? = null, degreeYearRange: IntRange? =
ResponseJson(
res,
title,
"Denne bedpres'en er kun åpen for ${degreeYearRange.start}- til ${degreeYearRange.last}-klasse.",
"Denne bedriftspresentasjonen er kun åpen for ${degreeYearRange.start}- til ${degreeYearRange.last}-klasse.",
date
)
}
24 changes: 17 additions & 7 deletions src/main/kotlin/no/uib/echo/plugins/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import io.ktor.http.*
import io.ktor.request.*
import io.ktor.response.*
import no.uib.echo.Response
import no.uib.echo.resToJson
import no.uib.echo.plugins.Routing.deleteBedpres
import no.uib.echo.plugins.Routing.deleteRegistration
import no.uib.echo.resToJson
import no.uib.echo.plugins.Routing.getRegistration
import no.uib.echo.plugins.Routing.getStatus
import no.uib.echo.plugins.Routing.postRegistration
Expand All @@ -27,6 +27,7 @@ import no.uib.echo.schema.Degree
import no.uib.echo.schema.RegistrationJson
import no.uib.echo.schema.RegistrationStatus
import no.uib.echo.schema.ShortRegistrationJson
import no.uib.echo.schema.countRegistrations
import no.uib.echo.schema.deleteBedpresBySlug
import no.uib.echo.schema.deleteRegistration
import no.uib.echo.schema.insertOrUpdateBedpres
Expand Down Expand Up @@ -100,12 +101,21 @@ object Routing {
val emailParam: String? = call.request.queryParameters["email"]
val slugParam: String? = call.request.queryParameters["slug"]

val result = selectRegistrations(emailParam, slugParam)

if (result != null)
call.respond(result)
else
call.respond(HttpStatusCode.BadRequest, "No email or slug given.")
when (call.request.queryParameters["count"]) {
"y", "Y" ->
if (slugParam != null)
call.respond(countRegistrations(slugParam))
else
call.respond(HttpStatusCode.BadRequest, "Count parameter defined but no slug was given.")
else -> {
val result = selectRegistrations(emailParam, slugParam)

if (result == null)
call.respond(HttpStatusCode.BadRequest, "No email or slug given.")
else
call.respond(result)
}
}
}
}

Expand Down
25 changes: 25 additions & 0 deletions src/main/kotlin/no/uib/echo/schema/Registration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ data class RegistrationJson(
val answers: List<AnswerJson>
)

data class RegistrationCountJson(
val regCount: Long,
val waitListCount: Long
)

data class ShortRegistrationJson(val slug: String, val email: String)

object Registration : Table() {
Expand Down Expand Up @@ -142,6 +147,26 @@ fun insertRegistration(reg: RegistrationJson): Triple<String?, IntRange?, Regist
}
}

fun countRegistrations(slug: String): RegistrationCountJson {
val regCount = transaction {
addLogger(StdOutSqlLogger)

Registration.select {
Registration.waitList eq false and (Registration.bedpresSlug eq slug)
}.count()
}

val waitListCount = transaction {
addLogger(StdOutSqlLogger)

Registration.select {
Registration.waitList eq true and (Registration.bedpresSlug eq slug)
}.count()
}

return RegistrationCountJson(regCount, waitListCount)
}

fun deleteRegistration(shortReg: ShortRegistrationJson) {
transaction {
addLogger(StdOutSqlLogger)
Expand Down
94 changes: 84 additions & 10 deletions src/test/kotlin/no/uib/echo/RegistrationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class RegistrationTest : StringSpec({
val res = gson.fromJson(submitRegCall.response.content, ResponseJson::class.java)
res.code shouldBe Response.OK
res.title shouldBe "Påmeldingen din er registrert!"
res.desc shouldBe ""
res.desc shouldBe "Du har fått plass på bedriftspresentasjonen."
}

for (b in bachelors) {
Expand Down Expand Up @@ -129,7 +129,7 @@ class RegistrationTest : StringSpec({
val res = gson.fromJson(submitRegCall.response.content, ResponseJson::class.java)
res.code shouldBe Response.OK
res.title shouldBe "Påmeldingen din er registrert!"
res.desc shouldBe ""
res.desc shouldBe "Du har fått plass på bedriftspresentasjonen."
}
}
}
Expand All @@ -149,7 +149,7 @@ class RegistrationTest : StringSpec({
val res = gson.fromJson(submitRegCall.response.content, ResponseJson::class.java)
res.code shouldBe Response.OK
res.title shouldBe "Påmeldingen din er registrert!"
res.desc shouldBe ""
res.desc shouldBe "Du har fått plass på bedriftspresentasjonen."
}
}

Expand All @@ -167,7 +167,7 @@ class RegistrationTest : StringSpec({
val res = gson.fromJson(submitRegCall.response.content, ResponseJson::class.java)
res.code shouldBe Response.OK
res.title shouldBe "Påmeldingen din er registrert!"
res.desc shouldBe ""
res.desc shouldBe "Du har fått plass på bedriftspresentasjonen."

val submitRegAgainCall: TestApplicationCall =
handleRequest(method = HttpMethod.Post, uri = "/${Routing.registrationRoute}") {
Expand Down Expand Up @@ -214,7 +214,7 @@ class RegistrationTest : StringSpec({
submitRegCall.response.status() shouldBe HttpStatusCode.Conflict
val res = gson.fromJson(submitRegCall.response.content, ResponseJson::class.java)
res.code shouldBe Response.BedpresDosntExist
res.title shouldBe "Denne bedpres'en finnes ikke."
res.title shouldBe "Denne bedriftspresentasjonen finnes ikke."
res.desc shouldBe "Om du mener dette ikke stemmer, ta kontakt med Webkom."
}
}
Expand Down Expand Up @@ -420,7 +420,7 @@ class RegistrationTest : StringSpec({
val res = gson.fromJson(submitRegCall.response.content, ResponseJson::class.java)
res.code shouldBe Response.OK
res.title shouldBe "Påmeldingen din er registrert!"
res.desc shouldBe ""
res.desc shouldBe "Du har fått plass på bedriftspresentasjonen."
}

for (i in 1..3) {
Expand All @@ -433,8 +433,8 @@ class RegistrationTest : StringSpec({
submitRegWaitlistCall.response.status() shouldBe HttpStatusCode.Accepted
val res = gson.fromJson(submitRegWaitlistCall.response.content, ResponseJson::class.java)
res.code shouldBe Response.WaitList
res.title shouldBe "Plassene er dessverre fylt opp..."
res.desc shouldBe "Du har blitt satt på venteliste."
res.title shouldBe "Alle plassene er dessverre fylt opp..."
res.desc shouldBe "Du har blitt satt på venteliste, og vil bli kontaktet om det åpner seg en ledig plass."
}
}
}
Expand Down Expand Up @@ -462,7 +462,7 @@ class RegistrationTest : StringSpec({
val res = gson.fromJson(submitRegCall.response.content, ResponseJson::class.java)
res.code shouldBe Response.NotInRange
res.title shouldBe "Du kan dessverre ikke melde deg på."
res.desc shouldBe "Denne bedpres'en er kun åpen for ${exampleBedpres4.minDegreeYear}- til ${exampleBedpres4.maxDegreeYear}-klasse."
res.desc shouldBe "Denne bedriftspresentasjonen er kun åpen for ${exampleBedpres4.minDegreeYear}- til ${exampleBedpres4.maxDegreeYear}-klasse."
}

for (i in 3..5) {
Expand All @@ -485,7 +485,7 @@ class RegistrationTest : StringSpec({
val res = gson.fromJson(submitRegCall.response.content, ResponseJson::class.java)
res.code shouldBe Response.NotInRange
res.title shouldBe "Du kan dessverre ikke melde deg på."
res.desc shouldBe "Denne bedpres'en er kun åpen for ${exampleBedpres5.minDegreeYear}- til ${exampleBedpres5.maxDegreeYear}-klasse."
res.desc shouldBe "Denne bedriftspresentasjonen er kun åpen for ${exampleBedpres5.minDegreeYear}- til ${exampleBedpres5.maxDegreeYear}-klasse."
}
}
}
Expand Down Expand Up @@ -535,4 +535,78 @@ class RegistrationTest : StringSpec({
}
}
}

"Should get correct count of registrations and wait list registrations" {
withTestApplication({
configureRouting(keys)
}) {
val waitListCount = 10

for (i in 1..(exampleBedpres1.spots + waitListCount)) {
val submitRegCall: TestApplicationCall =
handleRequest(method = HttpMethod.Post, uri = "/${Routing.registrationRoute}") {
addHeader(HttpHeaders.ContentType, "application/json")
setBody(
gson.toJson(
exampleReg.copy(
email = "ta123t${i}@test.com",
degree = Degree.DTEK,
degreeYear = 1
)
)
)
}

if (i > exampleBedpres1.spots) {
submitRegCall.response.status() shouldBe HttpStatusCode.Accepted
val res = gson.fromJson(submitRegCall.response.content, ResponseJson::class.java)
res.code shouldBe Response.WaitList
res.title shouldBe "Alle plassene er dessverre fylt opp..."
res.desc shouldBe "Du har blitt satt på venteliste, og vil bli kontaktet om det åpner seg en ledig plass."
} else {
submitRegCall.response.status() shouldBe HttpStatusCode.OK
val res = gson.fromJson(submitRegCall.response.content, ResponseJson::class.java)
res.code shouldBe Response.OK
res.title shouldBe "Påmeldingen din er registrert!"
res.desc shouldBe "Du har fått plass på bedriftspresentasjonen."
}
}

val getCountRegCall: TestApplicationCall =
handleRequest(
method = HttpMethod.Get,
uri = "/${Routing.registrationRoute}?count=y&slug=${exampleReg.slug}"
) {
addHeader(
HttpHeaders.Authorization,
"Basic ${Base64.getEncoder().encodeToString("$bedkom:${keys[bedkom]}".toByteArray())}"
)
}

getCountRegCall.response.status() shouldBe HttpStatusCode.OK
val res = gson.fromJson(getCountRegCall.response.content, RegistrationCountJson::class.java)
res.regCount shouldBe exampleBedpres1.spots
res.waitListCount shouldBe waitListCount
}
}

"Should respond properly when not given slug of bedpres when count of registrations are requested" {
withTestApplication({
configureRouting(keys)
}) {
val getCountRegCall: TestApplicationCall =
handleRequest(
method = HttpMethod.Get,
uri = "/${Routing.registrationRoute}?count=y"
) {
addHeader(
HttpHeaders.Authorization,
"Basic ${Base64.getEncoder().encodeToString("$bedkom:${keys[bedkom]}".toByteArray())}"
)
}

getCountRegCall.response.status() shouldBe HttpStatusCode.BadRequest
getCountRegCall.response.content shouldBe "Count parameter defined but no slug was given."
}
}
})

0 comments on commit 976bba4

Please sign in to comment.