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

[PAY-2924] Migrate getUser, supporter, followers endpoints to sdk #8555

Merged
merged 12 commits into from
May 23, 2024
3 changes: 1 addition & 2 deletions packages/common/src/api/signUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ const signUpApi = createApi({
const user = await userApiFetch.getUserByHandle(
{
handle,
currentUserId: null,
retry: false
currentUserId: null
},
context
)
Expand Down
33 changes: 21 additions & 12 deletions packages/common/src/api/user.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { full } from '@audius/sdk'

import { createApi } from '~/audius-query'
import { ID, Kind, StringUSDC } from '~/models'
import {
ID,
Kind,
OptionalId,
StringUSDC,
userMetadataListFromSDK
} from '~/models'
import {
USDCTransactionDetails,
USDCTransactionMethod,
Expand Down Expand Up @@ -46,10 +52,14 @@ const userApi = createApi({
getUserById: {
fetch: async (
{ id, currentUserId }: { id: ID; currentUserId: Nullable<ID> },
{ apiClient }
{ audiusSdk }
) => {
const apiUser = await apiClient.getUser({ userId: id, currentUserId })
return apiUser?.[0]
const sdk = await audiusSdk()
const { data: users = [] } = await sdk.full.users.getUser({
id: Id.parse(id),
userId: OptionalId.parse(currentUserId)
})
return userMetadataListFromSDK(users)[0]
},
options: {
idArgKey: 'id',
Expand All @@ -61,17 +71,16 @@ const userApi = createApi({
fetch: async (
{
handle,
currentUserId,
retry = true
}: { handle: string; currentUserId: Nullable<ID>; retry?: boolean },
{ apiClient }
currentUserId
}: { handle: string; currentUserId: Nullable<ID> },
{ audiusSdk }
) => {
const apiUser = await apiClient.getUserByHandle({
const sdk = await audiusSdk()
const { data: users = [] } = await sdk.full.users.getUserByHandle({
handle,
currentUserId,
retry
userId: OptionalId.parse(currentUserId)
})
return apiUser?.[0]
return userMetadataListFromSDK(users)[0]
},
options: {
kind: Kind.USERS,
Expand Down
3 changes: 3 additions & 0 deletions packages/common/src/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { z } from 'zod'

import { decodeHashId, encodeHashId } from '~/utils/hashIds'

// TODO: Move usage of these utils
// to the version in packages/common/src/models/Identifiers.ts
// https://linear.app/audius/issue/PAY-2997/migrate-idhashid-usage-to-models-version
Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense - should we add this to the migrations list somewhere? Just afraid we'll forget about this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The linked ticket is already in the SDK migration project, so it will have to be addressed before we finish.

export const HashId = z.string().transform<number>((data: string, context) => {
const id = decodeHashId(data)
if (id === null) {
Expand Down
40 changes: 40 additions & 0 deletions packages/common/src/models/Identifiers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { z } from 'zod'

import { decodeHashId, encodeHashId } from '~/utils/hashIds'

export type ID = number
export type UID = string
export type CID = string
Expand All @@ -7,3 +11,39 @@ export enum PlayableType {
PLAYLIST = 'playlist',
ALBUM = 'album'
}

export const HashId = z.string().transform<number>((data: string, context) => {
const id = decodeHashId(data)
if (id === null) {
context.addIssue({
code: z.ZodIssueCode.custom,
message: 'Hash id is invalid'
})

return z.NEVER
}
return id
})

/** Parses a `Nullable<ID>` safely for use with SDK functions which acccept an optional
id parameter.
*/
export const OptionalId = z
.number()
.nullable()
.transform<string | undefined>(
(data: number | null) => encodeHashId(data) ?? undefined
)

export const Id = z.number().transform<string>((data: number, context) => {
const id = encodeHashId(data)
if (id === null) {
context.addIssue({
code: z.ZodIssueCode.custom,
message: 'Hash id is invalid'
})

return z.NEVER
}
return id
})
46 changes: 46 additions & 0 deletions packages/common/src/models/Tipping.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { full } from '@audius/sdk'

import { ID } from '~/models/Identifiers'
import { removeNullable } from '~/utils/typeUtils'

import { UserMetadata, userMetadataFromSDK } from './User'
import { StringWei } from './Wallet'

export type Supporter = {
Expand Down Expand Up @@ -27,3 +31,45 @@ export type UserTip = {
export type LastDismissedTip = {
receiver_id: ID
}

export type SupporterMetadata = {
sender: UserMetadata
amount: StringWei
rank: number
}

export type SupportedUserMetadata = {
receiver: UserMetadata
amount: StringWei
rank: number
}

export const supporterMetadataFromSDK = (
input: full.FullSupporter
): SupporterMetadata | undefined => {
const user = userMetadataFromSDK(input.sender)
return user
? { sender: user, amount: input.amount as StringWei, rank: input.rank }
: undefined
}

export const supporterMetadataListFromSDK = (input?: full.FullSupporter[]) =>
input
? input.map((d) => supporterMetadataFromSDK(d)).filter(removeNullable)
: []

export const supportedUserMetadataFromSDK = (
input: full.FullSupporting
): SupportedUserMetadata | undefined => {
const user = userMetadataFromSDK(input.receiver)
return user
? { receiver: user, amount: input.amount as StringWei, rank: input.rank }
: undefined
}

export const supportedUserMetadataListFromSDK = (
input?: full.FullSupporting[]
) =>
input
? input.map((d) => supportedUserMetadataFromSDK(d)).filter(removeNullable)
: []
Loading