From ac0d295cf3476ac0163473fbb567fd780919c058 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Mon, 9 Oct 2023 22:06:35 +0800 Subject: [PATCH] debug --- packages/api/src/events/entity_created.ts | 37 -------- packages/api/src/events/user/user_created.ts | 92 ++++++++++---------- packages/api/src/services/create_user.ts | 40 ++++++--- packages/api/src/utils/sendEmail.ts | 2 +- 4 files changed, 74 insertions(+), 97 deletions(-) delete mode 100644 packages/api/src/events/entity_created.ts diff --git a/packages/api/src/events/entity_created.ts b/packages/api/src/events/entity_created.ts deleted file mode 100644 index 74b8c7abe..000000000 --- a/packages/api/src/events/entity_created.ts +++ /dev/null @@ -1,37 +0,0 @@ -/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ -import { PubSub } from '@google-cloud/pubsub' -import { - BaseEntity, - EntitySubscriberInterface, - EventSubscriber, - InsertEvent, -} from 'typeorm' -import { env } from '../env' -import { logger } from '../utils/logger' - -const TOPIC_NAME = 'EntityCreated' - -@EventSubscriber() -export class PublishEntitySubscriber implements EntitySubscriberInterface { - async afterInsert(event: InsertEvent): Promise { - const client = new PubSub() - - const msg = JSON.stringify({ - type: 'EntityCreated', - entity: event.entity, - entityClass: event.entity?.constructor?.name, - }) - - if (env.dev.isLocal) { - logger.info('PublishEntitySubscriber', msg) - return - } - - await client - .topic(TOPIC_NAME) - .publishMessage({ data: Buffer.from(msg) }) - .catch((err) => { - logger.error('PublishEntitySubscriber error publishing event', err) - }) - } -} diff --git a/packages/api/src/events/user/user_created.ts b/packages/api/src/events/user/user_created.ts index eb0fa1fc1..57d73fe1f 100644 --- a/packages/api/src/events/user/user_created.ts +++ b/packages/api/src/events/user/user_created.ts @@ -1,54 +1,54 @@ -import { - EntitySubscriberInterface, - EventSubscriber, - InsertEvent, -} from 'typeorm' -import { Profile } from '../../entity/profile' -import { createPubSubClient } from '../../pubsub' -import { addPopularReadsForNewUser } from '../../services/popular_reads' -import { IntercomClient } from '../../utils/intercom' +// import { +// EntitySubscriberInterface, +// EventSubscriber, +// InsertEvent, +// } from 'typeorm' +// import { Profile } from '../../entity/profile' +// import { createPubSubClient } from '../../pubsub' +// import { addPopularReadsForNewUser } from '../../services/popular_reads' +// import { IntercomClient } from '../../utils/intercom' -@EventSubscriber() -export class CreateIntercomAccount - implements EntitySubscriberInterface -{ - listenTo() { - return Profile - } +// @EventSubscriber() +// export class CreateIntercomAccount +// implements EntitySubscriberInterface +// { +// listenTo() { +// return Profile +// } - async afterInsert(event: InsertEvent): Promise { - const profile = event.entity +// async afterInsert(event: InsertEvent): Promise { +// const profile = event.entity - const customAttributes: { source_user_id: string } = { - source_user_id: profile.user.sourceUserId, - } - await IntercomClient?.contacts.createUser({ - email: profile.user.email, - externalId: profile.user.id, - name: profile.user.name, - avatar: profile.pictureUrl || undefined, - customAttributes: customAttributes, - signedUpAt: Math.floor(Date.now() / 1000), - }) - } -} +// const customAttributes: { source_user_id: string } = { +// source_user_id: profile.user.sourceUserId, +// } +// await IntercomClient?.contacts.createUser({ +// email: profile.user.email, +// externalId: profile.user.id, +// name: profile.user.name, +// avatar: profile.pictureUrl || undefined, +// customAttributes: customAttributes, +// signedUpAt: Math.floor(Date.now() / 1000), +// }) +// } +// } -@EventSubscriber() -export class PublishNewUserEvent implements EntitySubscriberInterface { - listenTo() { - return Profile - } +// @EventSubscriber() +// export class PublishNewUserEvent implements EntitySubscriberInterface { +// listenTo() { +// return Profile +// } - async afterInsert(event: InsertEvent): Promise { - const client = createPubSubClient() - await client.userCreated( - event.entity.user.id, - event.entity.user.email, - event.entity.user.name, - event.entity.username - ) - } -} +// async afterInsert(event: InsertEvent): Promise { +// const client = createPubSubClient() +// await client.userCreated( +// event.entity.user.id, +// event.entity.user.email, +// event.entity.user.name, +// event.entity.username +// ) +// } +// } // @EventSubscriber() // export class AddPopularReadsToNewUser diff --git a/packages/api/src/services/create_user.ts b/packages/api/src/services/create_user.ts index 24be963d9..c7add3e48 100644 --- a/packages/api/src/services/create_user.ts +++ b/packages/api/src/services/create_user.ts @@ -1,29 +1,22 @@ import { EntityManager } from 'typeorm' +import { Filter } from '../entity/filter' import { GroupMembership } from '../entity/groups/group_membership' import { Invite } from '../entity/groups/invite' import { Profile } from '../entity/profile' import { StatusType, User } from '../entity/user' +import { env } from '../env' import { SignupErrorCode } from '../generated/graphql' +import { createPubSubClient } from '../pubsub' import { authTrx, entityManager, getRepository } from '../repository' import { userRepository } from '../repository/user' import { AuthProvider } from '../routers/auth/auth_types' +import { analytics } from '../utils/analytics' +import { IntercomClient } from '../utils/intercom' import { logger } from '../utils/logger' import { validateUsername } from '../utils/usernamePolicy' +import { addPopularReadsForNewUser } from './popular_reads' import { sendConfirmationEmail } from './send_emails' -import { Filter } from '../entity/filter' -import { analytics } from '../utils/analytics' -import { env } from '../env' -const TOP_USERS = [ - 'jacksonh', - 'nat', - 'luis', - 'satindar', - 'malandrina', - 'patrick', - 'alexgutjahr', - 'hongbowu', -] export const MAX_RECORDS_LIMIT = 1000 export const createUser = async (input: { @@ -111,6 +104,7 @@ export const createUser = async (input: { } await createDefaultFiltersForUser(t)(user.id) + await addPopularReadsForNewUser(user.id, t) return [user, profile] } @@ -122,6 +116,26 @@ export const createUser = async (input: { } } + const customAttributes: { source_user_id: string } = { + source_user_id: user.sourceUserId, + } + await IntercomClient?.contacts.createUser({ + email: user.email, + externalId: user.id, + name: user.name, + avatar: profile.pictureUrl || undefined, + customAttributes: customAttributes, + signedUpAt: Math.floor(Date.now() / 1000), + }) + + const pubsubClient = createPubSubClient() + await pubsubClient.userCreated( + user.id, + user.email, + user.name, + profile.username + ) + analytics.track({ userId: user.id, event: 'create_user', diff --git a/packages/api/src/utils/sendEmail.ts b/packages/api/src/utils/sendEmail.ts index b99732099..ef5122be3 100644 --- a/packages/api/src/utils/sendEmail.ts +++ b/packages/api/src/utils/sendEmail.ts @@ -26,7 +26,7 @@ export const sendEmail = async (msg: MailDataRequired): Promise => { const client = new MailService() if (!process.env.SENDGRID_MSGS_API_KEY) { if (env.dev.isLocal) { - logger.error('SendGrid API key not set.\nSending email:', msg) + logger.info('SendGrid API key not set.\nSending email:', msg) return true }