From 24f1e953f695b86cf29d674906b047f6f97b37ec Mon Sep 17 00:00:00 2001 From: m5r Date: Sun, 3 Oct 2021 21:31:26 +0200 Subject: [PATCH] append messages from free users with "sent from shellphone" --- app/messages/api/queue/fetch-messages.ts | 5 ++-- .../api/queue/insert-incoming-message.ts | 2 +- app/messages/components/conversation.tsx | 2 +- app/messages/mutations/send-message.ts | 22 ++++++++++++--- app/messages/queries/get-conversation.ts | 28 ------------------- app/phone-calls/api/queue/fetch-calls.ts | 2 +- app/settings/pages/settings/billing.tsx | 1 - package-lock.json | 14 +++++----- package.json | 2 +- 9 files changed, 31 insertions(+), 47 deletions(-) delete mode 100644 app/messages/queries/get-conversation.ts diff --git a/app/messages/api/queue/fetch-messages.ts b/app/messages/api/queue/fetch-messages.ts index 2aafe23..74d6e22 100644 --- a/app/messages/api/queue/fetch-messages.ts +++ b/app/messages/api/queue/fetch-messages.ts @@ -1,4 +1,5 @@ import { Queue } from "quirrel/blitz"; +import type { MessageInstance } from "twilio/lib/rest/api/v2010/account/message"; import db from "../../../../db"; import insertMessagesQueue from "./insert-messages"; @@ -26,9 +27,7 @@ const fetchMessagesQueue = Queue("api/queue/fetch-messages", async ({ o ]); const messagesSent = sent.filter((message) => message.direction.startsWith("outbound")); const messagesReceived = received.filter((message) => message.direction === "inbound"); - const messages = [...messagesSent, ...messagesReceived].sort( - (a, b) => a.dateCreated.getTime() - b.dateCreated.getTime(), - ); + const messages = [...messagesSent, ...messagesReceived]; await insertMessagesQueue.enqueue( { diff --git a/app/messages/api/queue/insert-incoming-message.ts b/app/messages/api/queue/insert-incoming-message.ts index dd821b1..d864f71 100644 --- a/app/messages/api/queue/insert-incoming-message.ts +++ b/app/messages/api/queue/insert-incoming-message.ts @@ -33,7 +33,7 @@ const insertIncomingMessageQueue = Queue( from: message.from, status: translateMessageStatus(message.status), direction: translateMessageDirection(message.direction), - sentAt: message.dateCreated, + sentAt: new Date(message.dateCreated), content: encrypt(message.body, organization.encryptionKey), }, }); diff --git a/app/messages/components/conversation.tsx b/app/messages/components/conversation.tsx index 42e4f3a..1987b19 100644 --- a/app/messages/components/conversation.tsx +++ b/app/messages/components/conversation.tsx @@ -61,7 +61,7 @@ export default function Conversation() { > diff --git a/app/messages/mutations/send-message.ts b/app/messages/mutations/send-message.ts index 7f73fd4..be3b0a5 100644 --- a/app/messages/mutations/send-message.ts +++ b/app/messages/mutations/send-message.ts @@ -1,7 +1,7 @@ import { NotFoundError, resolver } from "blitz"; import { z } from "zod"; -import db, { Direction, MessageStatus } from "../../../db"; +import db, { Direction, MessageStatus, SubscriptionStatus } from "../../../db"; import { encrypt } from "../../../db/_encryption"; import sendMessageQueue from "../../messages/api/queue/send-message"; import appLogger from "../../../integrations/logger"; @@ -34,9 +34,23 @@ export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ const phoneNumber = organization.phoneNumbers[0]; if (!phoneNumber) { - return; + throw new NotFoundError(); } + const subscription = await db.subscription.findFirst({ + where: { + organizationId, + OR: [ + { status: { not: SubscriptionStatus.deleted } }, + { status: SubscriptionStatus.deleted, cancellationEffectiveDate: { gt: new Date() } }, + ], + }, + }); + + const isFreeSubscription = !subscription; + const messageBody = isFreeSubscription + ? content + "\n\nSent from Shellphone (https://www.shellphone.app)" + : content; const phoneNumberId = phoneNumber.id; const message = await db.message.create({ data: { @@ -46,7 +60,7 @@ export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ from: phoneNumber.number, direction: Direction.Outbound, status: MessageStatus.Queued, - content: encrypt(content, organization.encryptionKey), + content: encrypt(messageBody, organization.encryptionKey), sentAt: new Date(), }, }); @@ -57,7 +71,7 @@ export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ organizationId, phoneNumberId, to, - content, + content: messageBody, }, { id: `insert-${message.id}-${organizationId}-${phoneNumberId}`, diff --git a/app/messages/queries/get-conversation.ts b/app/messages/queries/get-conversation.ts deleted file mode 100644 index e5b7b01..0000000 --- a/app/messages/queries/get-conversation.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { NotFoundError, resolver } from "blitz"; -import { z } from "zod"; - -import db, { Prisma } from "../../../db"; -import { decrypt } from "../../../db/_encryption"; - -const GetConversations = z.object({ - recipient: z.string(), -}); - -export default resolver.pipe(resolver.zod(GetConversations), resolver.authorize(), async ({ recipient }, context) => { - const organization = await db.organization.findFirst({ where: { id: context.session.orgId } }); - if (!organization) { - throw new NotFoundError(); - } - - const conversation = await db.message.findMany({ - where: { OR: [{ from: recipient }, { to: recipient }] }, - orderBy: { sentAt: Prisma.SortOrder.desc }, - }); - - return conversation.map((message) => { - return { - ...message, - content: decrypt(message.content, organization.encryptionKey), - }; - }); -}); diff --git a/app/phone-calls/api/queue/fetch-calls.ts b/app/phone-calls/api/queue/fetch-calls.ts index 0abb8c9..baa877a 100644 --- a/app/phone-calls/api/queue/fetch-calls.ts +++ b/app/phone-calls/api/queue/fetch-calls.ts @@ -28,7 +28,7 @@ const fetchCallsQueue = Queue("api/queue/fetch-calls", async ({ organiz twilioClient.calls.list({ from: phoneNumber.number }), twilioClient.calls.list({ to: phoneNumber.number }), ]); - const calls = [...callsSent, ...callsReceived].sort((a, b) => a.dateCreated.getTime() - b.dateCreated.getTime()); + const calls = [...callsSent, ...callsReceived]; await insertCallsQueue.enqueue( { diff --git a/app/settings/pages/settings/billing.tsx b/app/settings/pages/settings/billing.tsx index 6ebe64b..0ff749e 100644 --- a/app/settings/pages/settings/billing.tsx +++ b/app/settings/pages/settings/billing.tsx @@ -24,7 +24,6 @@ const Billing: BlitzPage = (props) => { TODO: I want to be able to - upgrade to yearly - downgrade to monthly - - know how much time I have left until my cancelled subscription runs out --- DONE - resubscribe (message like "your subscription expired, would you like to renew ?") */ diff --git a/package-lock.json b/package-lock.json index 8ac74e4..25f4090 100644 --- a/package-lock.json +++ b/package-lock.json @@ -46,7 +46,7 @@ "remark": "14.0.1", "remark-html": "14.0.1", "tailwindcss": "2.2.15", - "twilio": "3.67.2", + "twilio": "3.68.0", "web-push": "3.4.5", "zod": "3.8.2" }, @@ -26862,9 +26862,9 @@ "integrity": "sha512-C3TaO7K81YvjCgQH9Q1S3R3P3BtN3RIM8n+OvX4il1K1zgE8ZhI0op7kClgkxtutIE8hQrcrHBXvIheqKUUCxw==" }, "node_modules/twilio": { - "version": "3.67.2", - "resolved": "https://registry.npmjs.org/twilio/-/twilio-3.67.2.tgz", - "integrity": "sha512-JOpnY+leRJIERFljSzKIIu80Kfj2QVIoldEjTDH1xiCEEwbmsMbZIzZwO/zDEhCpKcvpMTSuK2jl/xPmTxAkvA==", + "version": "3.68.0", + "resolved": "https://registry.npmjs.org/twilio/-/twilio-3.68.0.tgz", + "integrity": "sha512-xVFx/TbibpQtYwkDzuqPS8fsBGg8ZZ2iUtGU68dC9Dv1cngmxePcvxmyFxgPEd6wpnexJHHrCyiSr+LBaBEcDg==", "dependencies": { "axios": "^0.21.1", "dayjs": "^1.8.29", @@ -48649,9 +48649,9 @@ "integrity": "sha512-C3TaO7K81YvjCgQH9Q1S3R3P3BtN3RIM8n+OvX4il1K1zgE8ZhI0op7kClgkxtutIE8hQrcrHBXvIheqKUUCxw==" }, "twilio": { - "version": "3.67.2", - "resolved": "https://registry.npmjs.org/twilio/-/twilio-3.67.2.tgz", - "integrity": "sha512-JOpnY+leRJIERFljSzKIIu80Kfj2QVIoldEjTDH1xiCEEwbmsMbZIzZwO/zDEhCpKcvpMTSuK2jl/xPmTxAkvA==", + "version": "3.68.0", + "resolved": "https://registry.npmjs.org/twilio/-/twilio-3.68.0.tgz", + "integrity": "sha512-xVFx/TbibpQtYwkDzuqPS8fsBGg8ZZ2iUtGU68dC9Dv1cngmxePcvxmyFxgPEd6wpnexJHHrCyiSr+LBaBEcDg==", "requires": { "axios": "^0.21.1", "dayjs": "^1.8.29", diff --git a/package.json b/package.json index 4f81d83..eb7a690 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,7 @@ "remark": "14.0.1", "remark-html": "14.0.1", "tailwindcss": "2.2.15", - "twilio": "3.67.2", + "twilio": "3.68.0", "web-push": "3.4.5", "zod": "3.8.2" },