Skip to content

Commit

Permalink
chore(core): refactor merchant data structure
Browse files Browse the repository at this point in the history
chore: remove redundant describe

test: first integration test for add merchant

chore: add migration (untested yet)
  • Loading branch information
Nicolas Burtey committed Feb 1, 2024
1 parent ee42e71 commit b3f5a10
Show file tree
Hide file tree
Showing 34 changed files with 638 additions and 259 deletions.
7 changes: 7 additions & 0 deletions bats/admin-gql/business-delete-map-info.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mutation businessDeleteMapInfo($input: BusinessDeleteMapInfoInput!) {
businessDeleteMapInfo(input: $input) {
errors {
message
}
}
}
18 changes: 18 additions & 0 deletions bats/admin-gql/business-update-map-info.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
mutation businessUpdateMapInfo($input: BusinessUpdateMapInfoInput!) {
businessUpdateMapInfo(input: $input) {
errors {
message
}
merchant {
id
title
coordinates {
latitude
longitude
}
username
validated
createdAt
}
}
}
36 changes: 0 additions & 36 deletions bats/admin-gql/update-merchant-map.gql

This file was deleted.

24 changes: 21 additions & 3 deletions bats/core/api/merchant.bats
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ setup_file() {
login_admin
}

@test "merchants: add a merchant with admin api" {
@test "merchant: add a merchant with admin api" {
admin_token="$(read_value 'admin.token')"
latitude=40.712776
longitude=-74.005974
Expand All @@ -28,8 +28,8 @@ setup_file() {
'{input: {latitude: ($latitude | tonumber), longitude: ($longitude | tonumber), title: $title, username: $username}}'
)

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

Expand All @@ -39,3 +39,21 @@ setup_file() {
fetch_username="$(graphql_output '.data.businessMapMarkers[0].username')"
[[ $username = $fetch_username ]] || exit 1
}

@test "merchant: delete merchant with admin api" {
admin_token="$(read_value 'admin.token')"
local username="$(read_value merchant.username)"

variables=$(jq -n \
--arg username "$username" \
'{input: {username: $username}}'
)

exec_admin_graphql $admin_token 'business-delete-map-info' "$variables"

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
}
23 changes: 0 additions & 23 deletions core/api/src/app/accounts/delete-business-map-info.ts

This file was deleted.

6 changes: 0 additions & 6 deletions core/api/src/app/accounts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ export * from "./set-username"
export * from "./update-account-ip"
export * from "./update-account-level"
export * from "./update-account-status"
export * from "./update-business-map-info"
export * from "./update-contact-alias"
export * from "./update-default-walletid"
export * from "./update-display-currency"
export * from "./username-available"
export * from "./delete-business-map-info"
export * from "./upgrade-device-account"
export * from "./disable-notification-category"
export * from "./enable-notification-category"
Expand Down Expand Up @@ -51,7 +49,3 @@ export const hasPermissions = async (

return accountId === wallet.accountId
}

export const getBusinessMapMarkers = async () => {
return accounts.listBusinessesForMap()
}
8 changes: 6 additions & 2 deletions core/api/src/app/accounts/mark-account-for-deletion.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { deleteMerchantByUsername } from "../merchants"

import { getBalanceForWallet, listWalletsByAccountId } from "@/app/wallets"

import { AccountStatus, AccountValidator } from "@/domain/accounts"
Expand Down Expand Up @@ -62,8 +64,10 @@ export const markAccountForDeletion = async ({
status: AccountStatus.Closed,
updatedByPrivilegedClientId,
})
account.title = null
account.coordinates = null

if (account.username) {
await deleteMerchantByUsername({ username: account.username })
}

const newAccount = await accountsRepo.update(account)
if (newAccount instanceof Error) return newAccount
Expand Down
37 changes: 0 additions & 37 deletions core/api/src/app/accounts/update-business-map-info.ts

This file was deleted.

3 changes: 3 additions & 0 deletions core/api/src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as TransactionsMod from "./transactions"
import * as UsersMod from "./users"
import * as WalletsMod from "./wallets"
import * as PaymentsMod from "./payments"
import * as MerchantsMod from "./merchants"

import { wrapAsyncToRunInSpan } from "@/services/tracing"

Expand All @@ -21,6 +22,7 @@ const allFunctions = {
Callback: { ...CallbackMod },
Comm: { ...CommMod },
Quiz: { ...QuizMod },
Merchants: { ...MerchantsMod },
Lightning: { ...LightningMod },
OnChain: { ...OnChainMod },
Prices: { ...PricesMod },
Expand Down Expand Up @@ -50,6 +52,7 @@ export const {
Callback,
Comm,
Quiz,
Merchants,
Lightning,
OnChain,
Prices,
Expand Down
38 changes: 38 additions & 0 deletions core/api/src/app/merchants/delete-business-map-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { checkedToUsername } from "@/domain/accounts"
import { MerchantsRepository } from "@/services/mongoose"

export const deleteBusinessMapInfo = async ({
id,
}: {
id: MerchantId
}): Promise<void | ApplicationError> => {
const merchantsRepo = MerchantsRepository()

const result = await merchantsRepo.remove(id)
if (result instanceof Error) return result

return
}

export const deleteMerchantByUsername = async ({
username,
}: {
username: string
}): Promise<void | ApplicationError> => {
const merchantsRepo = MerchantsRepository()

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

const merchants = await merchantsRepo.findByUsername(usernameChecked)
if (merchants instanceof Error) return merchants

if (merchants.length === 0) return

for (const merchant of merchants) {
const result = await merchantsRepo.remove(merchant.id)
if (result instanceof Error) return result
}

return
}
10 changes: 10 additions & 0 deletions core/api/src/app/merchants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { MerchantsRepository } from "@/services/mongoose"

export * from "./update-business-map-info"
export * from "./delete-business-map-info"

const merchants = MerchantsRepository()

export const getMerchantsMapMarkers = async () => {
return merchants.listForMap()
}
45 changes: 45 additions & 0 deletions core/api/src/app/merchants/update-business-map-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { checkedCoordinates, checkedMapTitle, checkedToUsername } from "@/domain/accounts"
import { CouldNotFindMerchantFromUsernameError } from "@/domain/errors"
import { MerchantsRepository } from "@/services/mongoose"

export const updateBusinessMapInfo = 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 merchants = await merchantsRepo.findByUsername(usernameChecked)

if (merchants instanceof CouldNotFindMerchantFromUsernameError) {
return merchantsRepo.create({
username: usernameChecked,
coordinates,
title: titleChecked,
validated: true,
})
} else if (merchants instanceof Error) {
return merchants
}

// TODO: manage multiple merchants for a single username
const merchant = merchants[0]

merchant.coordinates = coordinates
merchant.title = titleChecked

return merchantsRepo.update(merchant)
}
19 changes: 0 additions & 19 deletions core/api/src/domain/accounts/index.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ type Account = {
level: AccountLevel
status: AccountStatus
statusHistory: AccountStatusHistory
title: BusinessMapTitle | null
coordinates: Coordinates | null
contactEnabled: boolean
readonly contacts: AccountContact[]
kratosUserId: UserId
Expand All @@ -113,22 +111,6 @@ type Account = {
role?: string
}

type BusinessMapTitle = string & { readonly brand: unique symbol }
type Coordinates = {
longitude: number
latitude: number
}

type BusinessMapInfo = {
title: BusinessMapTitle
coordinates: Coordinates
}

type BusinessMapMarker = {
username: Username
mapInfo: BusinessMapInfo
}

type AccountValidator = {
validateWalletForAccount(wallet: Wallet): true | ValidationError
}
Expand All @@ -142,7 +124,6 @@ interface IAccountsRepository {
persistNew(kratosUserId: UserId): Promise<Account | RepositoryError>

findByUsername(username: Username): Promise<Account | RepositoryError>
listBusinessesForMap(): Promise<BusinessMapMarker[] | RepositoryError>
update(account: Account): Promise<Account | RepositoryError>
}

Expand Down
2 changes: 2 additions & 0 deletions core/api/src/domain/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export class CouldNotFindLnPaymentFromHashError extends CouldNotFindError {}

export class CouldNotFindAccountFromIdError extends CouldNotFindError {}
export class CouldNotFindAccountFromUsernameError extends CouldNotFindError {}
export class CouldNotFindMerchantFromUsernameError extends CouldNotFindError {}
export class CouldNotFindMerchantFromIdError extends CouldNotFindError {}
export class CouldNotFindAccountFromPhoneError extends CouldNotFindError {}
export class CouldNotFindTransactionsForAccountError extends CouldNotFindError {}
export class CouldNotFindAccountFromKratosIdError extends CouldNotFindError {}
Expand Down
Loading

0 comments on commit b3f5a10

Please sign in to comment.