Skip to content

Commit

Permalink
feat: adding suggest and approve endpoint for the map
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Burtey committed Feb 3, 2024
1 parent 661b999 commit 5367f52
Show file tree
Hide file tree
Showing 24 changed files with 356 additions and 143 deletions.
11 changes: 11 additions & 0 deletions bats/admin-gql/merchant-map-validate.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mutation merchantMapValidate($input: MerchantMapValidateInput!) {
merchantMapValidate(input: $input) {
errors {
message
}
merchant {
id
validated
}
}
}
5 changes: 5 additions & 0 deletions bats/admin-gql/merchants-pending-approval.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query merchantsPendingApproval {
merchantsPendingApproval {
id
}
}
37 changes: 32 additions & 5 deletions bats/core/api/merchant.bats
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ setup_file() {
login_admin
}

@test "merchant: add a merchant with admin api" {
admin_token="$(read_value 'admin.token')"
@test "merchant: suggest a merchant" {
token_name='merchant'

latitude=40.712776
longitude=-74.005974
title="My Merchant"
Expand All @@ -28,19 +29,45 @@ setup_file() {
'{input: {latitude: ($latitude | tonumber), longitude: ($longitude | tonumber), title: $title, username: $username}}'
)

exec_admin_graphql $admin_token 'business-update-map-info' "$variables"
latitude_result="$(graphql_output '.data.businessUpdateMapInfo.merchant.coordinates.latitude')"
exec_graphql "$token_name" 'merchant-map-suggest' "$variables"
latitude_result="$(graphql_output '.data.merchantMapSuggest.merchant.coordinates.latitude')"
[[ "$latitude_result" == "$latitude" ]] || exit 1

# no merchant visible yet
exec_graphql 'anon' 'business-map-markers'
map_markers="$(graphql_output)"
markers_length=$(echo "$map_markers" | jq '.data.businessMapMarkers | length')

[[ $markers_length -eq 0 ]] || exit 1
}

@test "merchant: can query merchants" {
@test "merchant: listing and approving merchant waiting for approval" {
admin_token="$(read_value 'admin.token')"
local username="$(read_value merchant.username)"

exec_admin_graphql $admin_token 'merchants-pending-approval'
id="$(graphql_output '.data.merchantsPendingApproval[0].id')"
[[ "$id" != "null" && "$id" != "" ]] || exit 1

# validating merchant
variables=$(jq -n \
--arg id "$id" \
'{input: {id: $id}}'
)
exec_admin_graphql $admin_token 'merchant-map-validate' "$variables"
validate_status="$(graphql_output '.data.merchantMapValidate.merchant.validated')"
[[ "$validate_status" == "true" ]] || exit 1

# merchant is now visible from public api
local username="$(read_value merchant.username)"
exec_graphql 'anon' 'business-map-markers'
fetch_username="$(graphql_output '.data.businessMapMarkers[0].username')"
[[ $username = $fetch_username ]] || exit 1
}

@test "merchant: delete merchant with admin api" {
"skip"

admin_token="$(read_value 'admin.token')"
local username="$(read_value merchant.username)"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
mutation businessUpdateMapInfo($input: BusinessUpdateMapInfoInput!) {
businessUpdateMapInfo(input: $input) {
mutation merchantMapSuggest($input: MerchantMapSuggestInput!) {
merchantMapSuggest(input: $input) {
errors {
message
}
merchant {
id
validated
title
coordinates {
latitude
longitude
}
username
validated
createdAt
}
}
}
13 changes: 13 additions & 0 deletions core/api/src/app/merchants/approve-merchant-map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { MerchantsRepository } from "@/services/mongoose"

export const approveMerchantById = async (
id: MerchantId,
): Promise<BusinessMapMarker | ApplicationError> => {
const merchantsRepo = MerchantsRepository()

const merchant = await merchantsRepo.findById(id)
if (merchant instanceof Error) return merchant

merchant.validated = true
return merchantsRepo.update(merchant)
}
9 changes: 7 additions & 2 deletions core/api/src/app/merchants/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { MerchantsRepository } from "@/services/mongoose"

export * from "./update-business-map"
export * from "./delete-business-map"
export * from "./suggest-merchant-map"
export * from "./delete-merchant-map"
export * from "./approve-merchant-map"

const merchants = MerchantsRepository()

export const getMerchantsMapMarkers = async () => {
return merchants.listForMap()
}

export const getMerchantsPendingApproval = async () => {
return merchants.listPendingApproval()
}
36 changes: 36 additions & 0 deletions core/api/src/app/merchants/suggest-merchant-map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { checkedCoordinates, checkedMapTitle, checkedToUsername } from "@/domain/accounts"
import { AccountsRepository, MerchantsRepository } from "@/services/mongoose"

export const suggestMerchantMap = async ({
username,
coordinates: { latitude, longitude },
title,
}: {
username: string
coordinates: { latitude: number; longitude: number }
title: string
}): Promise<BusinessMapMarker | ApplicationError> => {
const merchantsRepo = MerchantsRepository()

const usernameChecked = checkedToUsername(username)
if (usernameChecked instanceof Error) return usernameChecked

const coordinates = checkedCoordinates({ latitude, longitude })
if (coordinates instanceof Error) return coordinates

const titleChecked = checkedMapTitle(title)
if (titleChecked instanceof Error) return titleChecked

const accountRepository = AccountsRepository()
const account = await accountRepository.findByUsername(usernameChecked)
if (account instanceof Error) {
return account
}

return merchantsRepo.create({
username: usernameChecked,
coordinates,
title: titleChecked,
validated: false,
})
}
45 changes: 0 additions & 45 deletions core/api/src/app/merchants/update-business-map.ts

This file was deleted.

8 changes: 4 additions & 4 deletions core/api/src/graphql/admin/mutations.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import UserUpdatePhoneMutation from "./root/mutation/user-update-phone"

import BusinessDeleteMapInfoMutation from "./root/mutation/delete-business-map"
import MerchantMapDeleteMutation from "./root/mutation/merchant-map-delete"
import MerchantMapValidateMutation from "./root/mutation/merchant-map-validate"

import AccountUpdateLevelMutation from "./root/mutation/account-update-level"
import AccountUpdateStatusMutation from "./root/mutation/account-update-status"
import AdminPushNotificationSendMutation from "./root/mutation/admin-push-notification-send"
import BusinessUpdateMapInfoMutation from "./root/mutation/business-update-map-info"

import { GT } from "@/graphql/index"

Expand All @@ -15,8 +15,8 @@ export const mutationFields = {
userUpdatePhone: UserUpdatePhoneMutation,
accountUpdateLevel: AccountUpdateLevelMutation,
accountUpdateStatus: AccountUpdateStatusMutation,
businessUpdateMapInfo: BusinessUpdateMapInfoMutation,
businessDeleteMapInfo: BusinessDeleteMapInfoMutation,
merchantMapValidate: MerchantMapValidateMutation,
merchantMapDelete: MerchantMapDeleteMutation,
adminPushNotificationSend: AdminPushNotificationSendMutation,
},
}
Expand Down
2 changes: 2 additions & 0 deletions core/api/src/graphql/admin/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ListWalletIdsQuery from "./root/query/all-walletids"
import WalletQuery from "./root/query/wallet"
import AccountDetailsByAccountId from "./root/query/account-details-by-account-id"
import AccountDetailsByUserId from "./root/query/account-details-by-user-id"
import MerchantsPendingApprovalQuery from "./root/query/merchants-pending-approval-listing"

import { GT } from "@/graphql/index"

Expand All @@ -28,6 +29,7 @@ export const queryFields = {
lightningPayment: LightningPaymentQuery,
listWalletIds: ListWalletIdsQuery,
wallet: WalletQuery,
merchantsPendingApproval: MerchantsPendingApprovalQuery,
},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import { Merchants } from "@/app"
import AccountDetailPayload from "@/graphql/admin/types/payload/account-detail"
import { mapAndParseErrorForGqlResponse } from "@/graphql/error-map"
import { GT } from "@/graphql/index"
import MerchantPayload from "@/graphql/shared/types/payload/merchant"
import Username from "@/graphql/shared/types/scalar/username"

const BusinessDeleteMapInfoInput = GT.Input({
name: "BusinessDeleteMapInfoInput",
const MerchantMapDeleteInput = GT.Input({
name: "MerchantMapDeleteInput",
fields: () => ({
username: {
type: GT.NonNull(Username),
},
}),
})

const BusinessDeleteMapInfoMutation = GT.Field<
const MerchantMapDeleteMutation = GT.Field<
null,
GraphQLAdminContext,
{
Expand All @@ -25,9 +26,9 @@ const BusinessDeleteMapInfoMutation = GT.Field<
extensions: {
complexity: 120,
},
type: GT.NonNull(AccountDetailPayload),
type: GT.NonNull(MerchantPayload),
args: {
input: { type: GT.NonNull(BusinessDeleteMapInfoInput) },
input: { type: GT.NonNull(MerchantMapDeleteInput) },
},
resolve: async (_, args) => {
const { username } = args.input
Expand All @@ -49,4 +50,4 @@ const BusinessDeleteMapInfoMutation = GT.Field<
},
})

export default BusinessDeleteMapInfoMutation
export default MerchantMapDeleteMutation
44 changes: 44 additions & 0 deletions core/api/src/graphql/admin/root/mutation/merchant-map-validate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Merchants } from "@/app"
import { mapAndParseErrorForGqlResponse } from "@/graphql/error-map"
import { GT } from "@/graphql/index"
import MerchantPayload from "@/graphql/shared/types/payload/merchant"

const MerchantMapValidateInput = GT.Input({
name: "MerchantMapValidateInput",
fields: () => ({
id: {
// TODO: MerchantID?
type: GT.NonNullID,
},
}),
})

const MerchantMapValidate = GT.Field<null, GraphQLAdminContext>({
extensions: {
complexity: 120,
},
type: GT.NonNull(MerchantPayload),
args: {
input: { type: GT.NonNull(MerchantMapValidateInput) },
},
resolve: async (_, args) => {
const { id } = args.input

if (id instanceof Error) {
return { errors: [{ message: id.message }] }
}

const merchant = await Merchants.approveMerchantById(id)

if (merchant instanceof Error) {
return { errors: [mapAndParseErrorForGqlResponse(merchant)] }
}

return {
errors: [],
merchant,
}
},
})

export default MerchantMapValidate
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { GT } from "@/graphql/index"

import { Merchants } from "@/app"
import { mapError } from "@/graphql/error-map"
import Merchant from "@/graphql/shared/types/object/merchant"

const MerchantsPendingApprovalQuery = GT.Field({
type: GT.List(Merchant),
resolve: async (_, { id }) => {
if (id instanceof Error) throw id

const merchants = await Merchants.getMerchantsPendingApproval()

if (merchants instanceof Error) {
throw mapError(merchants)
}

return merchants
},
})

export default MerchantsPendingApprovalQuery
Loading

0 comments on commit 5367f52

Please sign in to comment.