Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into tilgangsstyring_roller
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbb committed Oct 13, 2023
2 parents 950db3f + d45c59e commit aa9c509
Show file tree
Hide file tree
Showing 8 changed files with 1,105 additions and 28 deletions.
40 changes: 13 additions & 27 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ on:
workflow_dispatch:

jobs:
test:
name: Test
bygg:
name: Bygg
runs-on: ubuntu-latest
permissions:
contents: "read"
id-token: "write"
services:
postgres:
image: postgres:12.10
Expand All @@ -29,32 +32,15 @@ jobs:
java-version: 17
cache: maven

- run: mvn -B -Dsurefire.useFile=false test

bygg:
name: Bygg
needs: test
runs-on: ubuntu-latest
permissions:
contents: "read"
id-token: "write"
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 17
cache: maven

- run: mvn -B -DskipTests package
- run: mvn -B package

- uses: nais/docker-build-push@v0
id: gar-push
with:
team: fager
tag: ${{ github.sha }}
project_id: ${{ vars.NAIS_MANAGEMENT_PROJECT_ID }}
identity_provider: ${{ secrets.NAIS_WORKLOAD_IDENTITY_PROVIDER }}
- uses: nais/docker-build-push@v0
id: gar-push
with:
team: fager
tag: ${{ github.sha }}
project_id: ${{ vars.NAIS_MANAGEMENT_PROJECT_ID }}
identity_provider: ${{ secrets.NAIS_WORKLOAD_IDENTITY_PROVIDER }}

deploy-dev-gcp:
permissions:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.nav.arbeidsgiver.min_side.kontaktinfo

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import no.nav.arbeidsgiver.min_side.clients.retryInterceptor
import no.nav.arbeidsgiver.min_side.maskinporten.MaskinportenTokenService
Expand Down Expand Up @@ -78,6 +79,7 @@ class KontaktinfoClient(
)
}

@JsonIgnoreProperties(ignoreUnknown = true)
private class ContactInfoDTO(
@JsonProperty("MobileNumber")
val mobileNumber: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package no.nav.arbeidsgiver.min_side.varslingstatus

import no.nav.arbeidsgiver.min_side.controller.AuthenticatedUserHolder
import no.nav.arbeidsgiver.min_side.services.altinn.AltinnService
import no.nav.arbeidsgiver.min_side.varslingstatus.VarslingStatusDto.Status
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController
import java.time.Instant
import java.time.LocalDateTime

@RestController
class VarslingStatusController(
private val authenticatedUserHolder: AuthenticatedUserHolder,
private val altinnService: AltinnService,
private val repository: VarslingStatusRepository,
) {

@PostMapping("/api/varslingStatus/v1")
fun getVarslingStatus(@RequestBody requestBody: VarslingStatusRequest): VarslingStatus {
val virksomhetsnummer = requestBody.virksomhetsnummer
val harTilgang = altinnService.hentOrganisasjoner(authenticatedUserHolder.fnr)
.any { it.organizationNumber == virksomhetsnummer }

if (!harTilgang) {
return VarslingStatus(
status = Status.OK,
varselTimestamp = LocalDateTime.now(),
kvittertEventTimestamp = Instant.now(),
)
}

return repository.varslingStatus(virksomhetsnummer = virksomhetsnummer)
}

data class VarslingStatusRequest(
val virksomhetsnummer: String,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package no.nav.arbeidsgiver.min_side.varslingstatus

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.ObjectMapper
import no.nav.arbeidsgiver.min_side.varslingstatus.VarslingStatusDto.Status
import org.apache.kafka.clients.consumer.ConsumerRecord
import org.springframework.context.annotation.Profile
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
import org.springframework.kafka.annotation.KafkaListener
import org.springframework.stereotype.Repository
import org.springframework.stereotype.Service
import java.sql.PreparedStatement
import java.time.Instant
import java.time.LocalDateTime

data class VarslingStatus(
val status: Status,
val varselTimestamp: LocalDateTime,
val kvittertEventTimestamp: Instant,
)

@Repository
class VarslingStatusRepository(
private val jdbcTemplate: JdbcTemplate,
private val namedParameterJdbcTemplate: NamedParameterJdbcTemplate,
) {
fun varslingStatus(virksomhetsnummer: String): VarslingStatus {
return namedParameterJdbcTemplate.queryForList(
"""
select status, varslet_tidspunkt, status_tidspunkt
from varsling_status
where virksomhetsnummer = :virksomhetsnummer
order by status_tidspunkt desc
""".trimIndent(),
mapOf("virksomhetsnummer" to virksomhetsnummer)
).firstOrNull()?.let {
VarslingStatus(
status = Status.valueOf(it["status"] as String),
varselTimestamp = LocalDateTime.parse(it["varslet_tidspunkt"] as String),
kvittertEventTimestamp = Instant.parse(it["status_tidspunkt"] as String),
)
} ?: VarslingStatus(
status = Status.OK,
varselTimestamp = LocalDateTime.now(),
kvittertEventTimestamp = Instant.now(),
)
}

fun processVarslingStatus(varslingStatus: VarslingStatusDto) {
jdbcTemplate.update(
"""
insert into varsling_status(
varsel_id, virksomhetsnummer, status, status_tidspunkt, varslet_tidspunkt
) values(?, ?, ?, ?, ?)
on conflict (varsel_id)
-- upserter bare for sikkerhetsskyld, vil antakelig ikke skje
do update set
status = EXCLUDED.status,
status_tidspunkt = EXCLUDED.status_tidspunkt,
varslet_tidspunkt = EXCLUDED.varslet_tidspunkt;
""".trimIndent()
) { ps: PreparedStatement ->
ps.setString(1, varslingStatus.varselId)
ps.setString(2, varslingStatus.virksomhetsnummer)
ps.setString(3, varslingStatus.status.toString())
ps.setString(4, varslingStatus.eventTimestamp.toString())
ps.setString(5, varslingStatus.varselTimestamp.toString())
}
}
}

@Profile("dev-gcp", "prod-gcp")
@Service
class VarslingStatusKafkaListener(
private val varslingStatusRepository: VarslingStatusRepository,
private val objectMapper: ObjectMapper,
) {
@Profile("dev-gcp", "prod-gcp")
@KafkaListener(
id = "min-side-arbeidsgiver-varsling-status-1",
topics = ["fager.ekstern-varsling-status"],
containerFactory = "errorLoggingKafkaListenerContainerFactory"
)
fun processVarslingStatus(record: ConsumerRecord<String?, String?>) =
varslingStatusRepository.processVarslingStatus(
objectMapper.readValue(record.value(), VarslingStatusDto::class.java)
)
}

@JsonIgnoreProperties(ignoreUnknown = true)
data class VarslingStatusDto @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) constructor(
@param:JsonProperty("virksomhetsnummer") val virksomhetsnummer: String,
@param:JsonProperty("varselId") val varselId: String,
@param:JsonProperty("varselTimestamp") val varselTimestamp: LocalDateTime,
@param:JsonProperty("kvittertEventTimestamp") val eventTimestamp: Instant,
@param:JsonProperty("status") val status: Status,
@param:JsonProperty("version") val version: String,
) {
enum class Status {
OK,
MANGLER_KOFUVI,
ANNEN_FEIL,
}
}


12 changes: 12 additions & 0 deletions src/main/resources/db/migration/V10__varsling_status.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
create table varsling_status
(
varsel_id text not null primary key,
virksomhetsnummer text not null,
status text not null,
status_tidspunkt text not null,
varslet_tidspunkt text not null
);
create index varsling_status_virksomhetsnummer_idx on varsling_status (virksomhetsnummer);



Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import org.springframework.web.client.HttpClientErrorException
KontaktinfoClient::class,
MaskinportenTokenServiceStub::class,
)
@ActiveProfiles("local")
class KontaktinfoClientTest {
@Autowired
lateinit var altinnServer: MockRestServiceServer
Expand Down
Loading

0 comments on commit aa9c509

Please sign in to comment.