Skip to content

Commit

Permalink
Release 1.2.9 (#83)
Browse files Browse the repository at this point in the history
* T-29 Fix checked login abort error if path = login
* T-29 Add SuccessWarning when attempt add already favorite product to
favorites
* T-29 Fix rollback transaction to return warning
  • Loading branch information
mayer1a authored Oct 30, 2024
2 parents 93ae924 + 5dc80cd commit 311fe0a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
4 changes: 3 additions & 1 deletion Sources/App/Controllers/Favorites/FavoritesController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ struct FavoritesController: RouteCollection {
guard let user = request.auth.get(User.self) else { throw ErrorFactory.unauthorized() }

let productsIDs = try request.content.decode([UUID].self)
try await favoritesRepository.update(productsIDs: productsIDs, for: user)
if let error = try await favoritesRepository.update(productsIDs: productsIDs, for: user) {
throw error
}

return DummyResponse()
}
Expand Down
10 changes: 6 additions & 4 deletions Sources/App/Controllers/Favorites/FavoritesRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protocol FavoritesRepositoryProtocol: Sendable {
func get(for user: User, with page: PageRequest) async throws -> PaginationResponse<ProductDTO>
func get(for user: User) async throws -> [UUID]?
func getCount(for user: User) async throws -> FavoritesCountDTO
func update(productsIDs: [Product.IDValue], for user: User) async throws
func update(productsIDs: [Product.IDValue], for user: User) async throws -> CustomError?
func delete(productsIDs: [Product.IDValue], for user: User) async throws

}
Expand Down Expand Up @@ -47,21 +47,23 @@ final class FavoritesRepository: FavoritesRepositoryProtocol {
DTOFactory.makeFavoritesCount(from: try await buildQuery(for: user.requireID()).aggregate(.count, \.$id))
}

func update(productsIDs: [Product.IDValue], for user: User) async throws {
func update(productsIDs: [Product.IDValue], for user: User) async throws -> CustomError? {
try await database.transaction { transaction in
let products = try await Product.query(on: transaction)
.filter(\.$id ~~ productsIDs.reversed())
.all()

let favorites = try await user.$favorites.get(on: transaction)

var productAlreadyStarred = false
try await products.asyncForEach { product in
if try await favorites?.$products.isAttached(to: product, on: transaction) == true {
throw ErrorFactory.badRequest(.productAlreadyStarred)
productAlreadyStarred = true
} else {
try await favorites?.$products.attach(product, on: transaction)
}
}

return productAlreadyStarred ? ErrorFactory.successWarning(.productAlreadyStarred) : nil
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ enum BadRequest: String {
case oneProductUnavailable = "oneProductUnavailable"
case categoryIDRequired = "categoryIDRequired"
case categoryIDOrProductsIDsRequired = "categoryIDOrProductsIDsRequired"
case productAlreadyStarred = "productAlreadyStarred"
case xCityIdParameterRequired = "xCityIdParameterRequired"
case orderNumberRequired = "orderNumberRequired"
case orderIdRequired = "orderIdRequired"
Expand Down Expand Up @@ -57,9 +56,6 @@ extension BadRequest {
case .categoryIDOrProductsIDsRequired:
return "Category ID or products IDs is required."

case .productAlreadyStarred:
return "Attempting to add a product to favorites that has already been added."

case .xCityIdParameterRequired:
return "X-City-Id parameter is required."

Expand Down
22 changes: 22 additions & 0 deletions Sources/App/Helpers/ErrorFactory/ErrorFactory+SuccessWarning.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// ErrorFactory+SuccessWarning.swift
// MEOWLSServer
//
// Created by Artem Mayer on 29.10.2024.
//

import Foundation

enum SuccessWarning: String {

case productAlreadyStarred = "productAlreadyStarred"

var description: String {
switch self {
case .productAlreadyStarred:
return "Attempting to add a product to favorites that has already been added."

}
}

}
4 changes: 4 additions & 0 deletions Sources/App/Helpers/ErrorFactory/ErrorFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ struct ErrorFactory {
CustomError(.unauthorized, reason: "Access is denied. Please authenticate and try again.")
}

static func successWarning(_ type: SuccessWarning) -> CustomError {
CustomError(.alreadyReported, code: type.rawValue, reason: type.description)
}

static func internalError(_ type: InternalServerError, failures: [ValidationFailureType]? = nil) -> CustomError {
CustomError(.internalServerError, code: type.rawValue, reason: type.description, failures: failures?.toFailures)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ final class CustomErrorMiddleware: Middleware {
let code: String
let reason: String

if error.status == .unauthorized, url.path.pathComponents.last == "login" {
if url.path.pathComponents.contains("login") {

code = "authError"
reason = "Invalid phone or password"
Expand Down

0 comments on commit 311fe0a

Please sign in to comment.