-
Notifications
You must be signed in to change notification settings - Fork 90
/
index.ts
380 lines (362 loc) · 11 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import { isExisty } from "lib/helpers"
import date from "schema/v2/fields/date"
import initials from "schema/v2/fields/initials"
import { get, merge } from "lodash"
import {
GraphQLBoolean,
GraphQLList,
GraphQLObjectType,
GraphQLString,
GraphQLNonNull,
GraphQLEnumType,
GraphQLUnionType,
GraphQLFieldConfig,
} from "graphql"
import { pageable } from "relay-cursor-paging"
import { connectionFromArraySlice, connectionDefinitions } from "graphql-relay"
import { convertConnectionArgsToGravityArgs } from "lib/helpers"
import { ArtworkType } from "schema/v2/artwork"
import { ShowType } from "schema/v2/show"
import {
GlobalIDField,
NodeInterface,
InternalIDFields,
NullableIDField,
} from "schema/v2/object_identification"
import { MessageType } from "./message"
import { ResolverContext } from "types/graphql"
export const BuyerOutcomeTypes = new GraphQLEnumType({
name: "BuyerOutcomeTypes",
values: {
PURCHASED: {
value: "purchased",
},
STILL_CONSIDERING: {
value: "still_considering",
},
HIGH_PRICE: {
value: "high_price",
},
LOST_INTEREST: {
value: "lost_interest",
},
WORK_UNAVAILABLE: {
value: "work_unavailable",
},
OTHER: {
value: "other",
},
BLANK: {
value: "",
},
},
})
export const ConversationInitiatorType = new GraphQLObjectType<
any,
ResolverContext
>({
name: "ConversationInitiator",
description:
"The participant who started the conversation, currently always a User",
fields: {
...InternalIDFields,
type: {
description: "The type of participant, e.g. Partner or User",
type: new GraphQLNonNull(GraphQLString),
},
name: {
type: new GraphQLNonNull(GraphQLString),
},
email: {
type: new GraphQLNonNull(GraphQLString),
},
initials: initials("name"),
},
})
export const ConversationResponderType = new GraphQLObjectType<
any,
ResolverContext
>({
name: "ConversationResponder",
description:
"The participant responding to the conversation, currently always a Partner",
fields: {
...InternalIDFields,
type: {
description: "The type of participant, e.g. Partner or User",
type: new GraphQLNonNull(GraphQLString),
},
name: {
type: new GraphQLNonNull(GraphQLString),
},
replyToImpulseIDs: {
type: new GraphQLNonNull(new GraphQLList(GraphQLString)),
description:
"An array of Impulse IDs that correspond to all email addresses that messages should be sent to",
resolve: ({ reply_to_impulse_ids }) => reply_to_impulse_ids,
},
initials: initials("name"),
},
})
const ConversationItemType = new GraphQLUnionType({
name: "ConversationItemType",
types: [ArtworkType, ShowType],
resolveType: ({ __typename }) => {
switch (__typename) {
case "Artwork":
return ArtworkType
case "PartnerShow":
return ShowType
default:
return null
}
},
})
const ConversationItem = new GraphQLObjectType<any, ResolverContext>({
name: "ConversationItem",
fields: {
item: {
type: ConversationItemType,
resolve: ({ item_type, properties }) => ({
__typename: item_type,
...properties,
}),
},
title: {
type: GraphQLString,
},
permalink: {
type: GraphQLString,
},
},
})
export const {
connectionType: MessageConnection,
edgeType: MessageEdge,
} = connectionDefinitions({
nodeType: MessageType,
})
const isLastMessageToUser = ({ _embedded, from_email }) => {
const lastMessageFromEmail = get(_embedded, "last_message.from_email_address")
const lastMessagePrincipal = get(_embedded, "last_message.from_principal")
return lastMessagePrincipal === false || from_email !== lastMessageFromEmail
}
const lastMessageId = conversation => {
return get(conversation, "_embedded.last_message.id")
}
export const ConversationType = new GraphQLObjectType<any, ResolverContext>({
name: "Conversation",
description: "A conversation.",
interfaces: [NodeInterface],
fields: {
id: GlobalIDField,
...NullableIDField,
inquiryID: {
description: "Gravity inquiry id.",
type: GraphQLString,
resolve: ({ inquiry_id }) => inquiry_id,
},
from: {
description: "The participant who initiated the conversation",
type: new GraphQLNonNull(ConversationInitiatorType),
resolve: conversation => {
return {
id: conversation.from_id,
type: conversation.from_type,
name: conversation.from_name,
email: conversation.from_email,
}
},
},
to: {
description: "The participant(s) responding to the conversation",
type: new GraphQLNonNull(ConversationResponderType),
resolve: conversation => {
return {
id: conversation.to_id,
type: conversation.to_type,
name: conversation.to_name,
reply_to_impulse_ids: conversation.to,
}
},
},
buyerOutcome: {
type: GraphQLString,
resolve: ({ buyer_outcome }) => buyer_outcome,
},
buyerOutcomeAt: date,
createdAt: date,
fromLastViewedMessageID: {
type: GraphQLString,
resolve: ({ from_last_viewed_message_id }) => from_last_viewed_message_id,
},
initialMessage: {
type: new GraphQLNonNull(GraphQLString),
resolve: ({ initial_message, from_name }) => {
const parts = initial_message.split(
"Message from " + from_name + ":\n\n"
)
return parts[parts.length - 1]
},
},
lastMessage: {
type: GraphQLString,
description: "This is a snippet of text from the last message.",
resolve: () => null,
},
lastMessageAt: date,
lastMessageID: {
type: GraphQLString,
description: "Impulse id of the last message.",
resolve: conversation => lastMessageId(conversation),
},
// TODO: Currently if the user is not the sender of a message, we assume they are a recipient.
// That may not be the case, so we should evolve this check to be more accurate.
isLastMessageToUser: {
type: GraphQLBoolean,
description: "True if user/conversation initiator is a recipient.",
resolve: conversation => isLastMessageToUser(conversation),
},
// If the user is a recipient of the last message, return the relevant delivery id.
// This can be used to mark the message as read, or log other events.
lastMessageDeliveryID: {
type: GraphQLString,
description:
"Delivery id if the user is a recipient of the last message, null otherwise.",
resolve: (conversation, _options, { conversationMessagesLoader }) => {
if (!conversationMessagesLoader || !isLastMessageToUser(conversation)) {
return null
}
const radiationMessageId = get(
conversation,
"_embedded.last_message.radiation_message_id"
)
return conversationMessagesLoader({
conversation_id: conversation.id,
radiation_message_id: radiationMessageId,
"expand[]": "deliveries",
}).then(({ message_details }) => {
if (message_details.length === 0) {
return null
}
const relevantDelivery = message_details[0].deliveries.find(
d => d.email === conversation.from_email
)
if (!relevantDelivery) {
return null
}
return relevantDelivery.id
})
},
},
artworks: {
type: new GraphQLList(ArtworkType),
description: "Only the artworks discussed in the conversation.",
resolve: conversation => {
const results = []
for (const item of conversation.items) {
if (item.item_type === "Artwork") {
// FIXME: Argument of type 'any' is not assignable to parameter of type 'never'.
// @ts-ignore
results.push(item.properties)
}
}
return results
},
},
items: {
type: new GraphQLList(ConversationItem),
description:
"The artworks and/or partner shows discussed in the conversation.",
resolve: conversation => {
const results = []
for (const item of conversation.items) {
if (
isExisty(item.properties) &&
(item.item_type === "Artwork" || item.item_type === "PartnerShow")
) {
// FIXME: Argument of type 'any' is not assignable to parameter of type 'never'.
// @ts-ignore
results.push(item)
}
}
return results
},
},
messages: {
type: MessageConnection,
description: "A connection for all messages in a single conversation",
args: pageable({
sort: {
type: new GraphQLEnumType({
name: "sort",
values: {
DESC: { value: "desc" },
ASC: { value: "asc" },
},
}),
},
}),
resolve: (
{ id, from_email },
options,
{ conversationMessagesLoader }
) => {
if (!conversationMessagesLoader) return null
const { page, size, offset } = convertConnectionArgsToGravityArgs(
options
)
return conversationMessagesLoader({
page,
size,
conversation_id: id,
"expand[]": "deliveries",
sort: options.sort || "asc",
}).then(({ total_count, message_details }) => {
// Inject the convesation initiator's email into each message payload
// so we can tell if the user sent a particular message.
// Also inject the conversation id, since we need it in some message
// resolvers (invoices).
/* eslint-disable no-param-reassign */
message_details = message_details.map(message => {
return merge(message, {
conversation_from_address: from_email,
conversation_id: id,
})
})
/* eslint-disable no-param-reassign */
return connectionFromArraySlice(message_details, options, {
arrayLength: total_count,
sliceStart: offset,
})
})
},
},
unread: {
type: GraphQLBoolean,
description: "True if there is an unread message by the user.",
resolve: conversation => {
const memoizedLastMessageId = lastMessageId(conversation)
const { from_last_viewed_message_id } = conversation
return (
!!from_last_viewed_message_id &&
!!memoizedLastMessageId &&
from_last_viewed_message_id < memoizedLastMessageId
)
},
},
},
})
const Conversation: GraphQLFieldConfig<void, ResolverContext> = {
type: ConversationType,
description: "A conversation, usually between a user and a partner",
args: {
id: {
type: new GraphQLNonNull(GraphQLString),
description: "The ID of the Conversation",
},
},
resolve: (_root, { id }, { conversationLoader }) =>
conversationLoader ? conversationLoader(id) : null,
}
export default Conversation