@moneypot/hub 1.4.10 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -45,150 +45,152 @@ export const HubAuthenticatePlugin = makeExtendSchemaPlugin(() => {
45
45
  hubAuthenticate(input: HubAuthenticateInput!): HubAuthenticatePayload
46
46
  }
47
47
  `,
48
- plans: {
48
+ objects: {
49
49
  Mutation: {
50
- hubAuthenticate(_, { $input }) {
51
- try {
52
- const $context = context();
53
- const $success = sideEffect([$input, $context], ([rawInput, context]) => {
54
- return withPgPoolTransaction(superuserPool, async (pgClient) => {
55
- let input;
56
- try {
57
- input = InputSchema.parse(rawInput);
58
- }
59
- catch (e) {
60
- if (e instanceof z.ZodError) {
61
- throw new GraphQLError(e.errors[0].message);
50
+ plans: {
51
+ hubAuthenticate(_, { $input }) {
52
+ try {
53
+ const $context = context();
54
+ const $success = sideEffect([$input, $context], ([rawInput, context]) => {
55
+ return withPgPoolTransaction(superuserPool, async (pgClient) => {
56
+ let input;
57
+ try {
58
+ input = InputSchema.parse(rawInput);
62
59
  }
63
- throw e;
64
- }
65
- const { userToken: jwt, casinoBaseUrl } = input;
66
- const casino = await pgClient
67
- .query({
68
- text: `
60
+ catch (e) {
61
+ if (e instanceof z.ZodError) {
62
+ throw new GraphQLError(e.errors[0].message);
63
+ }
64
+ throw e;
65
+ }
66
+ const { userToken: jwt, casinoBaseUrl } = input;
67
+ const casino = await pgClient
68
+ .query({
69
+ text: `
69
70
  SELECT c.*, s.api_key
70
71
  FROM hub.casino c
71
72
  LEFT JOIN hub.casino_secret s ON c.id = s.id
72
73
  WHERE c.base_url = $1`,
73
- values: [casinoBaseUrl],
74
- })
75
- .then(maybeOneRow);
76
- if (!casino) {
77
- throw new GraphQLError(`hub server is unaware of casino with a base url of provided casinoBaseUrl`);
78
- }
79
- if (!casino.api_key) {
80
- throw new GraphQLError("Casino secret not configured");
81
- }
82
- const graphqlClient = createGraphqlClient({
83
- graphqlUrl: casino.graphql_url,
84
- apiKey: casino.api_key,
85
- });
86
- const verifyResult = await jwtService.verifyJwtFromDbCacheAndEnsureNotAlreadyUsed(pgClient, {
87
- casinoId: casino.id,
88
- jwt,
89
- });
90
- if (!verifyResult.ok) {
91
- throw new GraphQLError(`Error verifying userToken: ${verifyResult.error}`);
92
- }
93
- const { userToken } = verifyResult.value;
94
- let res;
95
- try {
96
- res = await graphqlClient.request(GET_USER_FROM_USER_TOKEN, {
97
- token: userToken,
74
+ values: [casinoBaseUrl],
75
+ })
76
+ .then(maybeOneRow);
77
+ if (!casino) {
78
+ throw new GraphQLError(`hub server is unaware of casino with a base url of provided casinoBaseUrl`);
79
+ }
80
+ if (!casino.api_key) {
81
+ throw new GraphQLError("Casino secret not configured");
82
+ }
83
+ const graphqlClient = createGraphqlClient({
84
+ graphqlUrl: casino.graphql_url,
85
+ apiKey: casino.api_key,
98
86
  });
99
- }
100
- catch (e) {
101
- logger.error(`[hubAuthenticate] Error when making GET_USER_FROM_USER_TOKEN to casino:`, e);
102
- if (isGraphQLError(e)) {
103
- const errorInfo = extractGraphQLErrorInfo(e);
104
- if (errorInfo.code === "UNAUTHENTICATED") {
105
- throw new GraphQLError("Invalid api key");
106
- }
107
- else {
108
- throw new GraphQLError(errorInfo.message);
87
+ const verifyResult = await jwtService.verifyJwtFromDbCacheAndEnsureNotAlreadyUsed(pgClient, {
88
+ casinoId: casino.id,
89
+ jwt,
90
+ });
91
+ if (!verifyResult.ok) {
92
+ throw new GraphQLError(`Error verifying userToken: ${verifyResult.error}`);
93
+ }
94
+ const { userToken } = verifyResult.value;
95
+ let res;
96
+ try {
97
+ res = await graphqlClient.request(GET_USER_FROM_USER_TOKEN, {
98
+ token: userToken,
99
+ });
100
+ }
101
+ catch (e) {
102
+ logger.error(`[hubAuthenticate] Error when making GET_USER_FROM_USER_TOKEN to casino:`, e);
103
+ if (isGraphQLError(e)) {
104
+ const errorInfo = extractGraphQLErrorInfo(e);
105
+ if (errorInfo.code === "UNAUTHENTICATED") {
106
+ throw new GraphQLError("Invalid api key");
107
+ }
108
+ else {
109
+ throw new GraphQLError(errorInfo.message);
110
+ }
109
111
  }
112
+ throw error;
110
113
  }
111
- throw error;
112
- }
113
- const result = res.userFromUserToken;
114
- if (!result || !result.user || !result.experience) {
115
- return null;
116
- }
117
- const mpUserId = result.user.id;
118
- assert(mpUserId);
119
- const uname = result.user.uname;
120
- assert(uname);
121
- const dbUser = await pgClient
122
- .query({
123
- text: `
114
+ const result = res.userFromUserToken;
115
+ if (!result || !result.user || !result.experience) {
116
+ return null;
117
+ }
118
+ const mpUserId = result.user.id;
119
+ assert(mpUserId);
120
+ const uname = result.user.uname;
121
+ assert(uname);
122
+ const dbUser = await pgClient
123
+ .query({
124
+ text: `
124
125
  INSERT INTO hub.user(casino_id, mp_user_id, uname)
125
126
  VALUES($1, $2, $3)
126
127
  ON CONFLICT (casino_id, mp_user_id) DO UPDATE
127
128
  SET uname = EXCLUDED.uname
128
129
  RETURNING id, uname
129
130
  `,
130
- values: [casino.id, mpUserId, uname],
131
- })
132
- .then(exactlyOneRow);
133
- const userId = dbUser.id;
134
- const mpExperience = result.experience;
135
- assert(mpExperience);
136
- const dbExperience = await pgClient
137
- .query({
138
- text: `
131
+ values: [casino.id, mpUserId, uname],
132
+ })
133
+ .then(exactlyOneRow);
134
+ const userId = dbUser.id;
135
+ const mpExperience = result.experience;
136
+ assert(mpExperience);
137
+ const dbExperience = await pgClient
138
+ .query({
139
+ text: `
139
140
  INSERT INTO hub.experience(casino_id, mp_experience_id, name)
140
141
  VALUES($1, $2, $3)
141
142
  ON CONFLICT (casino_id, mp_experience_id) DO UPDATE
142
143
  SET name = EXCLUDED.name
143
144
  RETURNING id
144
145
  `,
145
- values: [casino.id, mpExperience.id, mpExperience.name],
146
- })
147
- .then(exactlyOneRow);
148
- const dbSession = await pgClient
149
- .query({
150
- text: `
146
+ values: [casino.id, mpExperience.id, mpExperience.name],
147
+ })
148
+ .then(exactlyOneRow);
149
+ const dbSession = await pgClient
150
+ .query({
151
+ text: `
151
152
  INSERT INTO hub.session(casino_id, user_id, experience_id, user_token)
152
153
  VALUES($1, $2, $3, $4)
153
154
  RETURNING id, key
154
155
  `,
155
- values: [casino.id, userId, dbExperience.id, userToken],
156
- })
157
- .then(exactlyOneRow);
158
- const ret = {
159
- userId,
160
- uname: dbUser.uname,
161
- experienceId: dbExperience.id,
162
- sessionKey: dbSession.key,
163
- };
164
- context.identity = {
165
- kind: "user",
166
- session: {
167
- user_id: userId,
168
- mp_user_id: mpUserId,
169
- casino_id: casino.id,
170
- experience_id: dbExperience.id,
171
- session_id: dbSession.id,
172
- },
173
- };
174
- context.pgSettings = {
175
- "session.user_id": userId,
176
- "session.casino_id": casino.id,
177
- "session.experience_id": dbExperience.id,
178
- "session.session_id": dbSession.id,
179
- };
180
- return ret;
156
+ values: [casino.id, userId, dbExperience.id, userToken],
157
+ })
158
+ .then(exactlyOneRow);
159
+ const ret = {
160
+ userId,
161
+ uname: dbUser.uname,
162
+ experienceId: dbExperience.id,
163
+ sessionKey: dbSession.key,
164
+ };
165
+ context.identity = {
166
+ kind: "user",
167
+ session: {
168
+ user_id: userId,
169
+ mp_user_id: mpUserId,
170
+ casino_id: casino.id,
171
+ experience_id: dbExperience.id,
172
+ session_id: dbSession.id,
173
+ },
174
+ };
175
+ context.pgSettings = {
176
+ "session.user_id": userId,
177
+ "session.casino_id": casino.id,
178
+ "session.experience_id": dbExperience.id,
179
+ "session.session_id": dbSession.id,
180
+ };
181
+ return ret;
182
+ });
183
+ });
184
+ return object({
185
+ query: constant(true),
186
+ success: $success,
181
187
  });
182
- });
183
- return object({
184
- query: constant(true),
185
- success: $success,
186
- });
187
- }
188
- catch (error) {
189
- logger.error(error);
190
- throw error;
191
- }
188
+ }
189
+ catch (error) {
190
+ logger.error(error);
191
+ throw error;
192
+ }
193
+ },
192
194
  },
193
195
  },
194
196
  },
@@ -12,30 +12,34 @@ export const HubBalanceAlertPlugin = makeExtendSchemaPlugin(() => {
12
12
  currencyKey: String
13
13
  }
14
14
  `,
15
- plans: {
15
+ objects: {
16
16
  Subscription: {
17
- hubBalanceAlert: {
18
- subscribePlan(_$root) {
19
- const $pgSubscriber = context().get("pgSubscriber");
20
- const $identity = context().get("identity");
21
- const $channelKey = lambda($identity, (identity) => {
22
- if (identity?.kind === "user") {
23
- return `hub:user:${identity.session.user_id}:balance_alert`;
24
- }
25
- else {
26
- return "";
27
- }
28
- });
29
- return listen($pgSubscriber, $channelKey, jsonParse);
30
- },
31
- plan($event) {
32
- return $event;
17
+ plans: {
18
+ hubBalanceAlert: {
19
+ subscribePlan(_$root) {
20
+ const $pgSubscriber = context().get("pgSubscriber");
21
+ const $identity = context().get("identity");
22
+ const $channelKey = lambda($identity, (identity) => {
23
+ if (identity?.kind === "user") {
24
+ return `hub:user:${identity.session.user_id}:balance_alert`;
25
+ }
26
+ else {
27
+ return "";
28
+ }
29
+ });
30
+ return listen($pgSubscriber, $channelKey, jsonParse);
31
+ },
32
+ plan($event) {
33
+ return $event;
34
+ },
33
35
  },
34
36
  },
35
37
  },
36
38
  HubBalanceAlertPayload: {
37
- currencyKey($event) {
38
- return $event.get("currency_key");
39
+ plans: {
40
+ currencyKey($event) {
41
+ return $event.get("currency_key");
42
+ },
39
43
  },
40
44
  },
41
45
  },
@@ -16,59 +16,63 @@ export const HubClaimFaucetPlugin = makeExtendSchemaPlugin(() => {
16
16
  hubClaimFaucet: HubClaimFaucetPayload
17
17
  }
18
18
  `,
19
- plans: {
19
+ objects: {
20
20
  Mutation: {
21
- hubClaimFaucet() {
22
- const $identity = context().get("identity");
23
- const $result = sideEffect([$identity], ([identity]) => {
24
- if (identity?.kind !== "user") {
25
- throw new Error("Must be logged in as user");
26
- }
27
- const { session } = identity;
28
- return withPgPoolTransaction(superuserPool, async (pgClient) => {
29
- await upsertPlayCurrency(pgClient, session.casino_id);
30
- await pgClient.query({
31
- text: `
21
+ plans: {
22
+ hubClaimFaucet() {
23
+ const $identity = context().get("identity");
24
+ const $result = sideEffect([$identity], ([identity]) => {
25
+ if (identity?.kind !== "user") {
26
+ throw new Error("Must be logged in as user");
27
+ }
28
+ const { session } = identity;
29
+ return withPgPoolTransaction(superuserPool, async (pgClient) => {
30
+ await upsertPlayCurrency(pgClient, session.casino_id);
31
+ await pgClient.query({
32
+ text: `
32
33
  insert into hub.faucet_claim (user_id, casino_id, experience_id, currency_key, amount)
33
34
  values ($1, $2, $3, $4, $5)
34
35
  `,
35
- values: [
36
- session.user_id,
37
- session.casino_id,
38
- session.experience_id,
39
- "PLAY",
40
- CLAIM_AMOUNT,
41
- ],
42
- });
43
- await pgClient.query({
44
- text: `
36
+ values: [
37
+ session.user_id,
38
+ session.casino_id,
39
+ session.experience_id,
40
+ "PLAY",
41
+ CLAIM_AMOUNT,
42
+ ],
43
+ });
44
+ await pgClient.query({
45
+ text: `
45
46
  INSERT INTO hub.balance (user_id, experience_id, casino_id, currency_key, amount)
46
47
  VALUES ($1, $2, $3, $4, $5)
47
48
  ON CONFLICT (user_id, experience_id, casino_id, currency_key) DO UPDATE
48
49
  SET amount = balance.amount + EXCLUDED.amount
49
50
  `,
50
- values: [
51
- session.user_id,
52
- session.experience_id,
53
- session.casino_id,
54
- "PLAY",
55
- CLAIM_AMOUNT,
56
- ],
51
+ values: [
52
+ session.user_id,
53
+ session.experience_id,
54
+ session.casino_id,
55
+ "PLAY",
56
+ CLAIM_AMOUNT,
57
+ ],
58
+ });
59
+ return true;
57
60
  });
58
- return true;
59
61
  });
60
- });
61
- return object({
62
- result: $result,
63
- });
62
+ return object({
63
+ result: $result,
64
+ });
65
+ },
64
66
  },
65
67
  },
66
68
  HubClaimFaucetPayload: {
67
- success($data) {
68
- return $data.get("result");
69
- },
70
- query() {
71
- return constant(true);
69
+ plans: {
70
+ success($data) {
71
+ return $data.get("result");
72
+ },
73
+ query() {
74
+ return constant(true);
75
+ },
72
76
  },
73
77
  },
74
78
  },
@@ -1,4 +1,4 @@
1
- import { gql, makeExtendSchemaPlugin } from "graphile-utils";
1
+ import { gql, makeExtendSchemaPlugin } from "postgraphile/utils";
2
2
  import { context, inhibitOnNull, lambda } from "postgraphile/grafast";
3
3
  export const HubCurrentXPlugin = makeExtendSchemaPlugin((build) => {
4
4
  const userTable = build.input.pgRegistry.pgResources.hub_user;
@@ -14,47 +14,49 @@ export const HubCurrentXPlugin = makeExtendSchemaPlugin((build) => {
14
14
  hubCurrentSession: HubSession
15
15
  }
16
16
  `,
17
- plans: {
17
+ objects: {
18
18
  Query: {
19
- hubCurrentUser() {
20
- const $identity = context().get("identity");
21
- const $userId = lambda($identity, (identity) => {
22
- if (identity?.kind === "user") {
23
- return identity.session.user_id;
24
- }
25
- return null;
26
- });
27
- return userTable.get({ id: inhibitOnNull($userId) });
28
- },
29
- hubCurrentCasino() {
30
- const $identity = context().get("identity");
31
- const $casinoId = lambda($identity, (identity) => {
32
- if (identity?.kind === "user") {
33
- return identity.session.casino_id;
34
- }
35
- return null;
36
- });
37
- return casinoTable.get({ id: inhibitOnNull($casinoId) });
38
- },
39
- hubCurrentExperience() {
40
- const $identity = context().get("identity");
41
- const $experienceId = lambda($identity, (identity) => {
42
- if (identity?.kind === "user") {
43
- return identity.session.experience_id;
44
- }
45
- return null;
46
- });
47
- return experienceTable.get({ id: inhibitOnNull($experienceId) });
48
- },
49
- hubCurrentSession() {
50
- const $identity = context().get("identity");
51
- const $sessionId = lambda($identity, (identity) => {
52
- if (identity?.kind === "user") {
53
- return identity.session.session_id;
54
- }
55
- return null;
56
- });
57
- return sessionTable.get({ id: inhibitOnNull($sessionId) });
19
+ plans: {
20
+ hubCurrentUser() {
21
+ const $identity = context().get("identity");
22
+ const $userId = lambda($identity, (identity) => {
23
+ if (identity?.kind === "user") {
24
+ return identity.session.user_id;
25
+ }
26
+ return null;
27
+ });
28
+ return userTable.get({ id: inhibitOnNull($userId) });
29
+ },
30
+ hubCurrentCasino() {
31
+ const $identity = context().get("identity");
32
+ const $casinoId = lambda($identity, (identity) => {
33
+ if (identity?.kind === "user") {
34
+ return identity.session.casino_id;
35
+ }
36
+ return null;
37
+ });
38
+ return casinoTable.get({ id: inhibitOnNull($casinoId) });
39
+ },
40
+ hubCurrentExperience() {
41
+ const $identity = context().get("identity");
42
+ const $experienceId = lambda($identity, (identity) => {
43
+ if (identity?.kind === "user") {
44
+ return identity.session.experience_id;
45
+ }
46
+ return null;
47
+ });
48
+ return experienceTable.get({ id: inhibitOnNull($experienceId) });
49
+ },
50
+ hubCurrentSession() {
51
+ const $identity = context().get("identity");
52
+ const $sessionId = lambda($identity, (identity) => {
53
+ if (identity?.kind === "user") {
54
+ return identity.session.session_id;
55
+ }
56
+ return null;
57
+ });
58
+ return sessionTable.get({ id: inhibitOnNull($sessionId) });
59
+ },
58
60
  },
59
61
  },
60
62
  },