feat(messenger): store and serve telegram profiles
This commit is contained in:
@@ -11,6 +11,7 @@ import {
|
||||
import { sendLoginCodeEmail } from './mailer.js';
|
||||
import { dispatchToUserConnections, sendMessengerMessage } from './messenger.js';
|
||||
import { dateTimeScalar, jsonScalar } from './scalars.js';
|
||||
import { fetchTelegramConnectionProfile } from './telegram.js';
|
||||
|
||||
const ACTIVE_ORDER_STATUSES = ['NEW', 'MANAGER_PROCESSING', 'WAITING_DOUBLE_CONFIRM', 'CONFIRMED', 'IN_PROGRESS'];
|
||||
|
||||
@@ -133,6 +134,26 @@ function withDeliveryAddressDefaultFlag(address, defaultDeliveryAddressId) {
|
||||
};
|
||||
}
|
||||
|
||||
async function enrichMessengerConnectionProfile(prisma, connection) {
|
||||
if (
|
||||
connection.type !== 'TELEGRAM' ||
|
||||
(connection.displayName && connection.username && connection.avatarFileId)
|
||||
) {
|
||||
return connection;
|
||||
}
|
||||
|
||||
const profile = await fetchTelegramConnectionProfile(connection.channelId);
|
||||
return prisma.messengerConnection.update({
|
||||
where: { id: connection.id },
|
||||
data: {
|
||||
displayName: profile.displayName,
|
||||
username: profile.username,
|
||||
avatarFileId: profile.avatarFileId,
|
||||
avatarFileUniqueId: profile.avatarFileUniqueId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function resolveSelectedDeliveryAddress(context, userId, deliveryAddressId) {
|
||||
const normalizedAddressId = normalizeOptionalText(deliveryAddressId);
|
||||
|
||||
@@ -274,6 +295,9 @@ export const resolvers = {
|
||||
CounterpartyProfile: {
|
||||
isComplete: (profile) => isCounterpartyProfileComplete(profile),
|
||||
},
|
||||
MessengerConnection: {
|
||||
avatarAvailable: (connection) => Boolean(connection.avatarFileId),
|
||||
},
|
||||
DeliveryAddress: {
|
||||
isDefault: (address) => Boolean(address.isDefault),
|
||||
},
|
||||
@@ -308,10 +332,13 @@ export const resolvers = {
|
||||
|
||||
myMessengerConnections: async (_, __, context) => {
|
||||
const user = requireUser(context);
|
||||
return context.prisma.messengerConnection.findMany({
|
||||
const connections = await context.prisma.messengerConnection.findMany({
|
||||
where: { userId: user.id },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
return Promise.all(
|
||||
connections.map((connection) => enrichMessengerConnectionProfile(context.prisma, connection)),
|
||||
);
|
||||
},
|
||||
|
||||
myNotificationHistory: async (_, { channel, limit }, context) => {
|
||||
@@ -523,12 +550,22 @@ export const resolvers = {
|
||||
channelId: login.messengerConnection.channelId,
|
||||
},
|
||||
},
|
||||
update: { isActive: true },
|
||||
update: {
|
||||
isActive: true,
|
||||
displayName: login.messengerConnection.displayName ?? null,
|
||||
username: login.messengerConnection.username ?? null,
|
||||
avatarFileId: login.messengerConnection.avatarFileId ?? null,
|
||||
avatarFileUniqueId: login.messengerConnection.avatarFileUniqueId ?? null,
|
||||
},
|
||||
create: {
|
||||
userId: user.id,
|
||||
type: login.messengerConnection.type,
|
||||
channelId: login.messengerConnection.channelId,
|
||||
isActive: true,
|
||||
displayName: login.messengerConnection.displayName ?? null,
|
||||
username: login.messengerConnection.username ?? null,
|
||||
avatarFileId: login.messengerConnection.avatarFileId ?? null,
|
||||
avatarFileUniqueId: login.messengerConnection.avatarFileUniqueId ?? null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user