Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

購入申請作成APIを実装 #24

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import io.micronaut.security.filters.AuthenticationFetcher
import jakarta.inject.Singleton
import org.reactivestreams.Publisher
import reactor.core.publisher.Mono
import java.util.Base64
import java.util.*

/**
* AuthenticationFetcher
Expand All @@ -35,7 +35,8 @@ class ApplicationAuthenticationFetcher : AuthenticationFetcher {
val authorizationInfo = Base64.getDecoder().decode(authorization)
val userInfoResponse = ObjectMapper().readValue(authorizationInfo, UserInfoResponse::class.java)
val roles = userInfoResponse.roles
emitter.success(Authentication.build(userInfoResponse.id.toString(), roles))
val attributes = mapOf("userInfo" to userInfoResponse)
emitter.success(Authentication.build(userInfoResponse.id.toString(), roles, attributes))
} catch (exception: JsonProcessingException) {
emitter.error(UnauthorizedException(ErrorCode.USER_NOT_LOGGED_IN))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class PurchaseRequestModel(
/**
* 購入申請ID
*/
val id: UUID,
val id: UUID = UUID.randomUUID(),

/**
* 申請品名
Expand Down Expand Up @@ -51,6 +51,6 @@ class PurchaseRequestModel(
/**
* 申請日時
*/
val requestedAt: LocalDateTime
val requestedAt: LocalDateTime = LocalDateTime.now()

)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import java.util.*
* 購入申請リポジトリ
*/
interface PurchaseRequestRepository {
/**
* 購入申請を登録
*/
fun insert(purchaseRequestModel: PurchaseRequestModel)

/**
* idから購入申請を取得
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ enum class ErrorCode(val code: Int, val message: String) {
/**
* 30000~30999: 400 Bad Request
*/
INVALID_PURCHASE_REQUEST_NAME_LENGTH(30000, "購入申請品名は1文字以上255文字以下である必要があります。"),

INVALID_PURCHASE_REQUEST_DESCRIPTION_LENGTH(30001, "購入申請の説明文は1文字以上1023文字以下である必要があります。"),

INVALID_PURCHASE_REQUEST_PRICE(30002, "購入申請品の単価は1円以上である必要があります。"),

INVALID_PURCHASE_REQUEST_QUANTITY(30003, "購入申請品の個数は1個以上である必要があります。"),

INVALID_PURCHASE_REQUEST_URL_LENGTH(30004, "購入申請品名のURLは1文字以上1023文字以下である必要があります。"),

/**
* 31000~31999: 401 Unauthorized
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package cc.rits.membership.console.paymaster.infrastructure.api.controller

import cc.rits.membership.console.paymaster.annotation.RequestValidation
import cc.rits.membership.console.paymaster.infrastructure.api.request.UpsertPurchaseRequestRequest
import cc.rits.membership.console.paymaster.infrastructure.api.response.PurchaseRequestResponse
import cc.rits.membership.console.paymaster.infrastructure.api.response.PurchaseRequestsResponse
import cc.rits.membership.console.paymaster.usecase.purchase_request.CreatePurchaseRequestUseCase
import cc.rits.membership.console.paymaster.usecase.purchase_request.GetPurchaseRequestUseCase
import cc.rits.membership.console.paymaster.usecase.purchase_request.GetPurchaseRequestsUseCase
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.PathVariable
import io.micronaut.http.annotation.Post
import io.micronaut.scheduling.TaskExecutors
import io.micronaut.scheduling.annotation.ExecuteOn
import io.micronaut.security.annotation.Secured
import io.micronaut.security.authentication.Authentication
import io.micronaut.security.rules.SecurityRule
import io.swagger.v3.oas.annotations.Parameter
import io.swagger.v3.oas.annotations.parameters.RequestBody
import io.swagger.v3.oas.annotations.tags.Tag
import java.util.*

Expand All @@ -21,10 +28,24 @@ import java.util.*
@Controller("/api/purchase-requests")
@Secured(SecurityRule.IS_AUTHENTICATED)
@ExecuteOn(TaskExecutors.IO)
class PurchaseRequestRestController(
open class PurchaseRequestRestController(
private val createPurchaseRequestUseCase: CreatePurchaseRequestUseCase,
private val getPurchaseRequestsUseCase: GetPurchaseRequestsUseCase,
private val getPurchaseRequestUseCase: GetPurchaseRequestUseCase
) {
/**
* 購入申請作成API
*
* @param requestBody 購入申請作成リクエスト
* @param authentication 認証情報
*/
@Post
open fun createPurchaseRequest( //
@RequestBody @RequestValidation requestBody: UpsertPurchaseRequestRequest, //
@Parameter(hidden = true) authentication: Authentication //
) {
createPurchaseRequestUseCase.handle(authentication, requestBody)
}

/**
* 購入申請リスト取得API
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cc.rits.membership.console.paymaster.infrastructure.api.request

import cc.rits.membership.console.paymaster.exception.BadRequestException
import cc.rits.membership.console.paymaster.exception.ErrorCode
import cc.rits.membership.console.paymaster.util.ValidationUtil
import io.micronaut.core.annotation.Introspected

/**
* 購入申請作成リクエスト
*/
@Introspected
class UpsertPurchaseRequestRequest(
/**
* 申請品名
*/
val name: String,

/**
* 説明
*/
val description: String,

/**
* 単価
*/
val price: Int,

/**
* 個数
*/
val quantity: Int,

/**
* URL
*/
val url: String

) : BaseRequest() {
override fun validate() {
if (!ValidationUtil.checkStringLength(name, 1, 255)) {
throw BadRequestException(ErrorCode.INVALID_PURCHASE_REQUEST_NAME_LENGTH)
}

if (!ValidationUtil.checkStringLength(description, 1, 1023)) {
throw BadRequestException(ErrorCode.INVALID_PURCHASE_REQUEST_DESCRIPTION_LENGTH)
}

if (!ValidationUtil.checkNumberLength(price, 1, Int.MAX_VALUE)) {
throw BadRequestException(ErrorCode.INVALID_PURCHASE_REQUEST_PRICE)
}

if (!ValidationUtil.checkNumberLength(quantity, 1, Int.MAX_VALUE)) {
throw BadRequestException(ErrorCode.INVALID_PURCHASE_REQUEST_QUANTITY)
}

if (!ValidationUtil.checkStringLength(url, 1, 1023)) {
throw BadRequestException(ErrorCode.INVALID_PURCHASE_REQUEST_URL_LENGTH)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cc.rits.membership.console.paymaster.infrastructure.data.entity

import io.micronaut.data.annotation.GeneratedValue
import io.micronaut.data.annotation.Id
import io.micronaut.data.annotation.MappedEntity
import java.time.LocalDateTime
Expand All @@ -9,7 +8,6 @@ import java.util.*
@MappedEntity(value = "purchase_request")
data class PurchaseRequestEntity(
@Id
@GeneratedValue
val id: UUID,

val name: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import java.util.*

@JdbcRepository(dialect = Dialect.POSTGRES)
abstract class PurchaseRequestQuery : GenericRepository<PurchaseRequestEntity, UUID> {
abstract fun save(purchaseRequestEntity: PurchaseRequestEntity)

abstract fun findById(id: UUID): PurchaseRequestEntity?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import cc.rits.membership.console.paymaster.enums.PurchaseRequestStatus
import cc.rits.membership.console.paymaster.exception.ErrorCode
import cc.rits.membership.console.paymaster.exception.InternalServerErrorException
import cc.rits.membership.console.paymaster.factory.UserFactory
import cc.rits.membership.console.paymaster.infrastructure.data.entity.PurchaseRequestEntity
import cc.rits.membership.console.paymaster.util.AuthUtil
import jakarta.inject.Singleton
import java.util.*
Expand All @@ -18,6 +19,21 @@ class PurchaseRequestRepositoryImpl(
private val userFactory: UserFactory,
private val authUtil: AuthUtil
) : PurchaseRequestRepository {
override fun insert(purchaseRequestModel: PurchaseRequestModel) {
val purchaseRequestEntity = PurchaseRequestEntity(
id = purchaseRequestModel.id,
name = purchaseRequestModel.name,
description = purchaseRequestModel.description,
price = purchaseRequestModel.price,
quantity = purchaseRequestModel.quantity,
url = purchaseRequestModel.url,
status = purchaseRequestModel.status.id,
requestedBy = purchaseRequestModel.requestedBy!!.id,
requestedAt = purchaseRequestModel.requestedAt
)

purchaseRequestQuery.save(purchaseRequestEntity)
}

override fun findById(id: UUID): PurchaseRequestModel? {
return purchaseRequestQuery.findById(id)?.let {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cc.rits.membership.console.paymaster.usecase.purchase_request

import cc.rits.membership.console.paymaster.client.response.UserInfoResponse
import cc.rits.membership.console.paymaster.domain.model.PurchaseRequestModel
import cc.rits.membership.console.paymaster.domain.model.UserModel
import cc.rits.membership.console.paymaster.domain.repository.PurchaseRequestRepository
import cc.rits.membership.console.paymaster.enums.PurchaseRequestStatus
import cc.rits.membership.console.paymaster.enums.UserRole
import cc.rits.membership.console.paymaster.infrastructure.api.request.UpsertPurchaseRequestRequest
import io.micronaut.security.authentication.Authentication
import jakarta.inject.Singleton
import javax.transaction.Transactional

/**
* 購入申請作成ユースケース
*/
@Singleton
@Transactional
@Suppress("UNCHECKED_CAST")
open class CreatePurchaseRequestUseCase(
private val purchaseRequestRepository: PurchaseRequestRepository
) {
/**
* 購入申請を作成
*
* @param authentication 認証情報
* @param requestBody 購入申請作成リクエスト
*/
open fun handle(authentication: Authentication, requestBody: UpsertPurchaseRequestRequest) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[must] メール送信処理が抜けています。

  1. IAMにユーザリストを問い合わせ、PAYMASTERの管理者リストを取得
  2. 申請者 + PAYMASTERの管理者リストにメール送信

val userInfo = authentication.attributes["userInfo"] as UserInfoResponse
val userModel = UserModel(
id = userInfo.id,
firstName = userInfo.firstName,
lastName = userInfo.lastName,
entranceYear = userInfo.entranceYear,
roles = userInfo.userGroups.flatMap { userGroup ->
userGroup.roles.mapNotNull {
UserRole.findById(it)
}
}
)

val purchaseRequestModel = PurchaseRequestModel(
name = requestBody.name,
description = requestBody.description,
price = requestBody.price,
quantity = requestBody.quantity,
url = requestBody.url,
status = PurchaseRequestStatus.PENDING_APPROVAL,
requestedBy = userModel
)

purchaseRequestRepository.insert(purchaseRequestModel)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cc.rits.membership.console.paymaster.util

import io.micronaut.core.util.StringUtils

/**
* バリデーションユーティリティ
*/
class ValidationUtil {
companion object {
/**
* 文字列の長さが範囲に収まるかチェック
*
* @param string 文字列
* @param min 最小値
* @param max 最大値
*
* @return バリデーション結果
*/
fun checkStringLength(string: String, min: Int, max: Int): Boolean {
if (StringUtils.isEmpty(string) && min != 0) {
return false
}

return string.length in min..max
}

/**
* 数値が範囲に収まるかチェック
*
* @param value 数値
* @param min 最小値
* @param max 最大値
*
* @return バリデーション結果
*/
fun checkNumberLength(value: Int, min: Int, max: Int): Boolean {
return value in min..max
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Specification

@MicronautTest
@MicronautTest(transactional = false)
abstract class BaseSpec extends Specification {

@Inject
Expand Down
Loading