append messages from free users with "sent from shellphone"

This commit is contained in:
m5r 2021-10-03 21:31:26 +02:00
parent a28d89a8c2
commit 24f1e953f6
9 changed files with 31 additions and 47 deletions

View File

@ -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<Payload>("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(
{

View File

@ -33,7 +33,7 @@ const insertIncomingMessageQueue = Queue<Payload>(
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),
},
});

View File

@ -61,7 +61,7 @@ export default function Conversation() {
>
<span
className={clsx(
"inline-block text-left w-[fit-content] p-2 rounded-lg text-white",
"inline-block whitespace-pre-line text-left w-[fit-content] p-2 rounded-lg text-white",
isOutbound ? "bg-[#3194ff] rounded-br-none" : "bg-black rounded-bl-none",
)}
>

View File

@ -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}`,

View File

@ -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),
};
});
});

View File

@ -28,7 +28,7 @@ const fetchCallsQueue = Queue<Payload>("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(
{

View File

@ -24,7 +24,6 @@ const Billing: BlitzPage<Props> = (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 ?")
*/

14
package-lock.json generated
View File

@ -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",

View File

@ -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"
},