From 1c1682fe4f044f22ed2ab97cdd45c1abf16f3f5e Mon Sep 17 00:00:00 2001 From: Matt Low Date: Thu, 11 Mar 2021 12:34:12 -0700 Subject: [PATCH] Move duration column to recordings table --- ...85912_move_duration_to_recordings_table.ts | 47 +++++++++++++++++++ src/template.tsx | 7 ++- src/ticketize.ts | 2 +- src/types.ts | 2 +- 4 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 src/db/migrations/20210311185912_move_duration_to_recordings_table.ts diff --git a/src/db/migrations/20210311185912_move_duration_to_recordings_table.ts b/src/db/migrations/20210311185912_move_duration_to_recordings_table.ts new file mode 100644 index 0000000..e0ee501 --- /dev/null +++ b/src/db/migrations/20210311185912_move_duration_to_recordings_table.ts @@ -0,0 +1,47 @@ +import { Knex } from "knex"; + +export async function up(knex: Knex) { + return knex.transaction(async (trx) => { + await trx.schema.alterTable("recordings", (table) => { + table.integer("duration"); + }); + + // transfer recording durations from voicemails table + await trx("recordings").update({ + duration: knex("voicemails") + .select("duration") + .where("messageId", knex.raw("??", "recordings.messageId")), + }); + + // now we can make duration column not-nullable + await trx.schema.alterTable("recordings", (table) => { + table.integer("duration").notNullable().alter(); + }); + + await trx.schema.alterTable("voicemails", (table) => { + table.dropColumn("duration"); + }); + }); +} + +export async function down(knex: Knex) { + return knex.transaction(async (trx) => { + await trx.schema.alterTable("voicemails", (table) => { + table.integer("duration"); + }); + + await trx("voicemails").update({ + duration: knex("recordings") + .select("duration") + .where("messageId", knex.raw("??", "voicemails.messageId")), + }); + + await trx.schema.alterTable("voicemails", (table) => { + table.integer("duration").notNullable().alter(); + }); + + await trx.schema.alterTable("recordings", (table) => { + table.dropColumn("duration"); + }); + }); +} diff --git a/src/template.tsx b/src/template.tsx index 0d22936..3fd6e46 100644 --- a/src/template.tsx +++ b/src/template.tsx @@ -3,7 +3,7 @@ import ReactDOMServer from "react-dom/server"; import { getNationalNumber, formatSeconds } from "./util"; import { DateTime } from "luxon"; import type { Contact } from "./types"; -import type { StoredVoicemail } from "knex/types/tables"; +import type { StoredVoicemail, StoredRecording } from "knex/types/tables"; export function getTicketSubject( voicemail: StoredVoicemail, @@ -14,7 +14,10 @@ export function getTicketSubject( })`; } -export function getTicketBody(vm: StoredVoicemail, contact?: Contact) { +export function getTicketBody( + vm: StoredVoicemail & StoredRecording, + contact?: Contact +) { return ReactDOMServer.renderToStaticMarkup(
diff --git a/src/ticketize.ts b/src/ticketize.ts index 40d38c6..3ab5314 100644 --- a/src/ticketize.ts +++ b/src/ticketize.ts @@ -243,7 +243,6 @@ export function ticketize( extensionName: extension.name, fromNumber: message.from.phoneNumber, fromName: message.from.name, - duration: recording.duration, transcriptionStatus: transcription.status, transcription: transcription.text, }); @@ -251,6 +250,7 @@ export function ticketize( messageId: message.id, mimeType: recording.mimeType, audio: new Uint8Array(recording.audio), + duration: recording.duration, }); }); } diff --git a/src/types.ts b/src/types.ts index fdcfa1a..662e728 100644 --- a/src/types.ts +++ b/src/types.ts @@ -116,7 +116,6 @@ declare module "knex/types/tables" { extensionName: string; fromNumber: string; fromName: string; - duration: number; transcriptionStatus: TranscriptionStatus; transcription: string | null; ticketId?: number; @@ -129,6 +128,7 @@ declare module "knex/types/tables" { messageId: number; mimeType: string; audio: ArrayBuffer; + duration: number; } interface Tables {