diff --git a/src/gql/heart-monitor/heart-monitor.test.ts b/src/gql/heart-monitor/heart-monitor.test.ts index c253ad0f..d86a020c 100644 --- a/src/gql/heart-monitor/heart-monitor.test.ts +++ b/src/gql/heart-monitor/heart-monitor.test.ts @@ -98,8 +98,9 @@ import { defaultTask, QueryMarketingArgs, MarketingFields, - GQLMarketingMutationGqlUpdateTwitterUserArgs, - GQLTwitterUser, + QueryMarketingMutationArgs, + GQLMarketingMutationFields, + defaultAccountLinksInfo, } from ".." const nibiruUrl = "testnet-1" @@ -436,8 +437,8 @@ test("markPriceCandlesSubscription", async () => { }) const testMarketingMutation = async ( - args: GQLMarketingMutationGqlUpdateTwitterUserArgs, - fields?: GQLTwitterUser + args: QueryMarketingMutationArgs, + fields?: GQLMarketingMutationFields ) => { const resp = await heartMonitor.marketingMutation(args, {}, fields) expect(resp).toHaveProperty("marketing") @@ -445,34 +446,29 @@ const testMarketingMutation = async ( if (resp.marketing) { const { marketing } = resp - checkFields( - [marketing], - [ - "completedTasks", - "creationTimestamp", - "displayName", - "followersCount", - "followingCount", - "id", - "likes", - "listedCount", - "nibiAddress", - "tweets", - "tweetsCount", - "username", - ] - ) + checkFields([marketing], ["updateTwitterUser", "linkAccounts"]) } } // Create JIT JWT for this test test.skip("marketingMutation", async () => { await testMarketingMutation({ - input: { - userId: "800528778854182912", - nibiAddress: "nibi1p6luzkxeufy29reymgjqnl5mv6a6gae07cphed", - displayName: "WildFyre", - username: "wildfyreapp", + updateTwitterUser: { + input: { + userId: "800528778854182912", + nibiAddress: "nibi1p6luzkxeufy29reymgjqnl5mv6a6gae07cphed", + displayName: "WildFyre", + username: "wildfyreapp", + }, + }, + linkAccounts: { + input: { + nibiAddress: "nibi1p6luzkxeufy29reymgjqnl5mv6a6gae07cphed", + twitterUser: { + displayName: "WildFyre", + username: "wildfyreapp", + }, + }, }, }) }) @@ -489,7 +485,15 @@ const testMarketingQuery = async ( checkFields( [marketing], - ["likes", "retweets", "tasks", "tweets", "twitterUser"] + [ + "likes", + "retweets", + "tasks", + "tweets", + "twitterUser", + "accountLinksInfo", + "lastUpdatedTimestamp", + ] ) } } @@ -506,6 +510,12 @@ test.skip("marketingQuery", async () => { likes: { where: { userId: "1516130689028087815" }, }, + accountLinksInfo: { + where: { + nibiAddress: "nibi1p6luzkxeufy29reymgjqnl5mv6a6gae07cphed", + twitterId: "800528778854182912", + }, + }, }) await testMarketingQuery( { @@ -518,21 +528,31 @@ test.skip("marketingQuery", async () => { likes: { where: { userId: "" }, }, + accountLinksInfo: { + where: { + nibiAddress: "", + twitterId: "", + }, + }, }, { + accountLinksInfo: defaultAccountLinksInfo, twitterUser: defaultTwitterUser, tweets: defaultTweet, likes: defaultLike, tasks: defaultTask, + lastUpdatedTimestamp: "", } ) await testMarketingQuery( {}, { + accountLinksInfo: defaultAccountLinksInfo, twitterUser: defaultTwitterUser, tweets: defaultTweet, likes: defaultLike, tasks: defaultTask, + lastUpdatedTimestamp: "", } ) }) diff --git a/src/gql/heart-monitor/heart-monitor.ts b/src/gql/heart-monitor/heart-monitor.ts index 397a24e0..28a5fe74 100644 --- a/src/gql/heart-monitor/heart-monitor.ts +++ b/src/gql/heart-monitor/heart-monitor.ts @@ -112,6 +112,8 @@ import { GqlOutMarketingMutation, GQLTwitterUser, DeepPartial, + QueryMarketingMutationArgs, + GQLMarketingMutationFields, } from ".." /** IHeartMonitor is an interface for a Heart Monitor GraphQL API. @@ -154,9 +156,9 @@ export interface IHeartMonitor { ) => Promise readonly marketingMutation: ( - args: GQLMarketingMutationGqlUpdateTwitterUserArgs, + args: QueryMarketingMutationArgs, headers: HeadersInit, - fields?: DeepPartial + fields?: DeepPartial ) => Promise readonly marketingQuery: ( @@ -338,9 +340,9 @@ export class HeartMonitor implements IHeartMonitor { ) => marketingQuery(args, this.gqlEndpt, fields) marketingMutation = async ( - args: GQLMarketingMutationGqlUpdateTwitterUserArgs, + args: QueryMarketingMutationArgs, headers: HeadersInit, - fields?: DeepPartial + fields?: DeepPartial ) => marketingMutation(args, this.gqlEndpt, headers, fields) markPriceCandles = async ( diff --git a/src/gql/mutation/marketing.ts b/src/gql/mutation/marketing.ts index a7fe0737..5bafa8de 100644 --- a/src/gql/mutation/marketing.ts +++ b/src/gql/mutation/marketing.ts @@ -7,18 +7,79 @@ import { GQLTwitterUser, GQLMarketingMutationGqlUpdateTwitterUserArgs, DeepPartial, + GQLMarketingMutationGqlLinkAccountsArgs, + GQLAccountLinksInfo, + defaultAccountLinksInfo, } from ".." +export type QueryMarketingMutationArgs = { + updateTwitterUser?: GQLMarketingMutationGqlUpdateTwitterUserArgs + linkAccounts?: GQLMarketingMutationGqlLinkAccountsArgs +} + export interface GqlOutMarketingMutation { marketing?: GQLMutation["marketing"] } +export type GQLMarketingMutationFields = DeepPartial<{ + updateTwitterUser?: DeepPartial + linkAccounts?: DeepPartial +}> + export const marketingMutationString = ( - args: GQLMarketingMutationGqlUpdateTwitterUserArgs, + args: QueryMarketingMutationArgs, excludeParentObject: boolean, - fields?: DeepPartial -) => - `mutation { + fields?: DeepPartial +) => { + const GQLMarketingMutationQuery: string[] = [] + + if (fields?.updateTwitterUser) { + GQLMarketingMutationQuery.push( + gqlQuery( + "updateTwitterUser", + args.updateTwitterUser ?? {}, + convertObjectToPropertiesString(fields.updateTwitterUser), + true + ) + ) + } + + if (fields?.linkAccounts) { + GQLMarketingMutationQuery.push( + gqlQuery( + "linkAccounts", + args.linkAccounts ?? {}, + convertObjectToPropertiesString(fields.linkAccounts), + true + ) + ) + } + + // Default Objects + + if (args.updateTwitterUser && !fields?.updateTwitterUser) { + GQLMarketingMutationQuery.push( + gqlQuery( + "updateTwitterUser", + args.updateTwitterUser, + convertObjectToPropertiesString(defaultTwitterUser), + true + ) + ) + } + + if (args.linkAccounts && !fields?.linkAccounts) { + GQLMarketingMutationQuery.push( + gqlQuery( + "linkAccounts", + args.linkAccounts, + convertObjectToPropertiesString(defaultAccountLinksInfo), + true + ) + ) + } + + return `mutation { marketing { ${gqlQuery( "updateTwitterUser", @@ -30,11 +91,12 @@ export const marketingMutationString = ( )} } }` +} export const marketingMutation = async ( - args: GQLMarketingMutationGqlUpdateTwitterUserArgs, + args: QueryMarketingMutationArgs, endpt: string, headers: HeadersInit, - fields?: DeepPartial + fields?: DeepPartial ): Promise => doGqlQuery(marketingMutationString(args, true, fields), endpt, headers) diff --git a/src/gql/query/marketing.ts b/src/gql/query/marketing.ts index b2a186cf..ee095cd8 100644 --- a/src/gql/query/marketing.ts +++ b/src/gql/query/marketing.ts @@ -1,12 +1,15 @@ import { convertObjectToPropertiesString, DeepPartial, + defaultAccountLinksInfo, defaultLike, defaultTask, defaultTweet, defaultTwitterUser, doGqlQuery, + GQLAccountLinksInfo, GQLLike, + GQLMarketingQueryGqlAccountLinksInfoArgs, GQLMarketingQueryGqlLikesArgs, GQLMarketingQueryGqlTasksArgs, GQLMarketingQueryGqlTweetsArgs, @@ -23,6 +26,7 @@ export type QueryMarketingArgs = { tweets?: GQLMarketingQueryGqlTweetsArgs likes?: GQLMarketingQueryGqlLikesArgs tasks?: GQLMarketingQueryGqlTasksArgs + accountLinksInfo?: GQLMarketingQueryGqlAccountLinksInfoArgs } export interface GqlOutMarketingQuery { @@ -34,6 +38,8 @@ export type MarketingFields = DeepPartial<{ tweets?: DeepPartial likes?: DeepPartial tasks?: DeepPartial + accountLinksInfo?: DeepPartial + lastUpdatedTimestamp?: string }> export const marketingQueryString = ( @@ -86,6 +92,17 @@ export const marketingQueryString = ( ) } + if (fields?.accountLinksInfo) { + marketingQuery.push( + gqlQuery( + "accountLinksInfo", + args.accountLinksInfo ?? {}, + convertObjectToPropertiesString(fields.accountLinksInfo), + true + ) + ) + } + // Default Objects if (args.likes && !fields?.likes) { @@ -132,6 +149,22 @@ export const marketingQueryString = ( ) } + if (args.accountLinksInfo && !fields?.accountLinksInfo) { + marketingQuery.push( + gqlQuery( + "accountLinksInfo", + args.accountLinksInfo, + convertObjectToPropertiesString(defaultAccountLinksInfo), + true + ) + ) + } + + // Add lastUpdatedTimestamp if specified + if (fields?.lastUpdatedTimestamp) { + marketingQuery.push("lastUpdatedTimestamp") + } + return ` marketing { ${marketingQuery.join("\n")} diff --git a/src/gql/utils/consts.ts b/src/gql/utils/consts.ts index dd29f0c2..264101d2 100644 --- a/src/gql/utils/consts.ts +++ b/src/gql/utils/consts.ts @@ -60,13 +60,13 @@ export const checkFields = (objects: T[], fields: string[]) => { export const queryBatchHandler = async ( queryQueryStrings: string[], - endpt: string, + endpt: string ) => doGqlQuery(`{ ${queryQueryStrings.join("\n")} }`, endpt) export const arg = ( name: string, value: unknown, - ignoreQuotes?: boolean, + ignoreQuotes?: boolean ) => { const isString = typeof value === "string" && !ignoreQuotes ? `"` : "" @@ -101,7 +101,7 @@ export const getWhereArgArr = (whereArgs: IterableDictionary) => `where: ${objToGql(whereArgs)}` export const convertObjectToPropertiesString = ( - obj: IterableDictionary, + obj: IterableDictionary ) => { let result = "" @@ -115,14 +115,14 @@ export const convertObjectToPropertiesString = ( ${Object.keys(item) .map((k) => `${k}`) .join("\n")} - }`, + }` ) .join("\n") result += `${innerString}\n` } else if (typeof value === "object" && value !== null) { result += `${key} { ${convertObjectToPropertiesString( - value as IterableDictionary, + value as IterableDictionary )} }\n` } else { @@ -151,20 +151,20 @@ export const gqlQuery = ( name: string, typedQueryArgs: IterableDictionary, properties: string, - excludeParentObject?: boolean, + excludeParentObject?: boolean ) => { const queryArgList = [] if (typedQueryArgs.where !== undefined) { queryArgList.push( - getWhereArgArr(typedQueryArgs.where as IterableDictionary), + getWhereArgArr(typedQueryArgs.where as IterableDictionary) ) } delete typedQueryArgs.where Object.keys(typedQueryArgs).forEach((key) => - queryArgList.push(arg(key, typedQueryArgs[key], true)), + queryArgList.push(arg(key, typedQueryArgs[key], true)) ) const hasQueryList = (char: string) => (queryArgList.length > 0 ? char : "") @@ -179,7 +179,7 @@ export const gqlQuery = ( export const doGqlQuery = async ( gqlQuery: string, gqlEndpt: string, - headers?: HeadersInit, + headers?: HeadersInit ) => { const rawResp = await fetch(gqlEndpt, { method: "POST", diff --git a/src/gql/utils/defaultObjects.ts b/src/gql/utils/defaultObjects.ts index 05c2402d..1cd61d26 100644 --- a/src/gql/utils/defaultObjects.ts +++ b/src/gql/utils/defaultObjects.ts @@ -1,4 +1,5 @@ import { + GQLAccountLinksInfo, GQLBlock, GQLDelegation, GQLDistributionCommission, @@ -452,11 +453,9 @@ export const defaultTwitterUser: GQLTwitterUser = { displayName: "", followersCount: 0, followingCount: 0, - discordId: "", id: "", likes: [], listedCount: 0, - nibiAddress: "", tweets: [], tweetsCount: 0, username: "", @@ -503,3 +502,9 @@ export const defaultTaskCompletion: GQLTaskCompletion = { task: defaultTask, user: defaultTwitterUser, } + +export const defaultAccountLinksInfo: GQLAccountLinksInfo = { + nibiAddress: "", + discordId: "", + twitterUser: defaultTwitterUser, +}