|
| 1 | +package org.gitanimals.identity.controller.admin |
| 2 | + |
| 3 | +import org.gitanimals.core.admin.AdminCallDetected |
| 4 | +import org.gitanimals.core.admin.AdminConst.ADMIN_SECRET_KEY |
| 5 | +import org.gitanimals.identity.app.Token |
| 6 | +import org.gitanimals.identity.app.UserFacade |
| 7 | +import org.gitanimals.identity.controller.admin.request.PointDecreaseRequest |
| 8 | +import org.gitanimals.identity.controller.admin.request.PointIncreaseRequest |
| 9 | +import org.gitanimals.identity.domain.EntryPoint |
| 10 | +import org.gitanimals.identity.domain.UserService |
| 11 | +import org.springframework.beans.factory.annotation.Value |
| 12 | +import org.springframework.context.ApplicationEventPublisher |
| 13 | +import org.springframework.http.HttpHeaders |
| 14 | +import org.springframework.http.HttpStatus |
| 15 | +import org.springframework.web.bind.annotation.* |
| 16 | + |
| 17 | +@RestController |
| 18 | +class AdminUserController( |
| 19 | + private val userService: UserService, |
| 20 | + private val userFacade: UserFacade, |
| 21 | + private val eventPublisher: ApplicationEventPublisher, |
| 22 | + @Value("\${gitanimals.admin.token}") private val adminToken: String, |
| 23 | +) { |
| 24 | + |
| 25 | + @ResponseStatus(HttpStatus.OK) |
| 26 | + @PostMapping("/admin/users/points/increase/by-username/{username}") |
| 27 | + fun increaseUserPointsByUsername( |
| 28 | + @PathVariable("username") username: String, |
| 29 | + @RequestParam( |
| 30 | + name = "entryPoint", |
| 31 | + defaultValue = "GITHUB", |
| 32 | + ) |
| 33 | + entryPoint: EntryPoint = EntryPoint.GITHUB, |
| 34 | + @RequestHeader(ADMIN_SECRET_KEY) adminSecret: String, |
| 35 | + @RequestHeader(HttpHeaders.AUTHORIZATION) authorization: String, |
| 36 | + @RequestBody pointIncreaseRequest: PointIncreaseRequest, |
| 37 | + ) { |
| 38 | + require(adminSecret == adminToken) { |
| 39 | + "WRONG TOKEN" |
| 40 | + } |
| 41 | + |
| 42 | + val user = userFacade.getUserByToken(Token.from(authorization)) |
| 43 | + |
| 44 | + userService.increasePointByUsernameWithoutIdempotency(username, entryPoint, pointIncreaseRequest.point) |
| 45 | + eventPublisher.publishEvent( |
| 46 | + AdminCallDetected( |
| 47 | + username = user.getName(), |
| 48 | + reason = pointIncreaseRequest.reason, |
| 49 | + path = "/admin/users/points/increase/by-username/{username}", |
| 50 | + description = "$username 유저에게 ${pointIncreaseRequest.point} 지급", |
| 51 | + ) |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + @ResponseStatus(HttpStatus.OK) |
| 56 | + @PostMapping("/admin/users/points/decrease/by-username/{username}") |
| 57 | + fun decreaseUserPointsByUsername( |
| 58 | + @PathVariable("username") username: String, |
| 59 | + @RequestParam( |
| 60 | + name = "entryPoint", |
| 61 | + defaultValue = "GITHUB", |
| 62 | + ) |
| 63 | + entryPoint: EntryPoint = EntryPoint.GITHUB, |
| 64 | + @RequestHeader(ADMIN_SECRET_KEY) adminSecret: String, |
| 65 | + @RequestHeader(HttpHeaders.AUTHORIZATION) authorization: String, |
| 66 | + @RequestBody pointDecreaseRequest: PointDecreaseRequest, |
| 67 | + ) { |
| 68 | + require(adminSecret == adminToken) { |
| 69 | + "WRONG TOKEN" |
| 70 | + } |
| 71 | + |
| 72 | + val user = userFacade.getUserByToken(Token.from(authorization)) |
| 73 | + |
| 74 | + userService.decreasePointByUsernameWithoutIdempotency(username, entryPoint, pointDecreaseRequest.point) |
| 75 | + eventPublisher.publishEvent( |
| 76 | + AdminCallDetected( |
| 77 | + username = user.getName(), |
| 78 | + reason = pointDecreaseRequest.reason, |
| 79 | + path = "/admin/users/points/decrease/by-username/{username}", |
| 80 | + description = "$username 유저에게 ${pointDecreaseRequest.point} 차감", |
| 81 | + ) |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments