-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create CHES email api logging functionality
Create email_log table Create typescript EmailLog types Create service function for logging Add log function to email service functions
- Loading branch information
Showing
7 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* eslint-disable max-len */ | ||
import stamps from '../stamps'; | ||
|
||
import type { Knex } from 'knex'; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
return Promise.resolve().then(() => | ||
// Create public schema tables | ||
knex.schema | ||
.createTable('email_log', (table) => { | ||
table.uuid('email_id').primary(); | ||
table.integer('http_status').defaultTo(null); | ||
table.uuid('msg_id').defaultTo(null); | ||
table.text('to').defaultTo(null); | ||
table.uuid('tx_id').defaultTo(null); | ||
stamps(knex, table); | ||
}) | ||
|
||
// Create before update triggers | ||
.then(() => | ||
knex.schema.raw(`CREATE TRIGGER before_update_email_log_trigger | ||
BEFORE UPDATE ON email_log | ||
FOR EACH ROW EXECUTE PROCEDURE public.set_updated_at();`) | ||
) | ||
|
||
// Create audit triggers | ||
.then(() => | ||
knex.schema.raw(`CREATE TRIGGER audit_email_log_trigger | ||
AFTER UPDATE OR DELETE ON email_log | ||
FOR EACH ROW EXECUTE PROCEDURE audit.if_modified_func();`) | ||
) | ||
); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
return ( | ||
Promise.resolve() | ||
// Drop audit triggers | ||
.then(() => knex.schema.raw('DROP TRIGGER IF EXISTS audit_email_log_trigger ON email_log')) | ||
|
||
// Drop public schema table triggers | ||
.then(() => knex.schema.raw('DROP TRIGGER IF EXISTS before_update_email_log_trigger ON email_log')) | ||
|
||
// Drop public schema tables | ||
.then(() => knex.schema.dropTableIfExists('email_log')) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Prisma } from '@prisma/client'; | ||
|
||
import type { EmailLog } from '../../types'; | ||
|
||
// Define types | ||
const _emailLog = Prisma.validator<Prisma.email_logDefaultArgs>()({}); | ||
const _emailLogWithGraph = Prisma.validator<Prisma.email_logDefaultArgs>()({}); | ||
|
||
type PrismaRelationEmailLog = Prisma.email_logGetPayload<typeof _emailLog>; | ||
type PrismaGraphEmailLog = Prisma.email_logGetPayload<typeof _emailLogWithGraph>; | ||
|
||
export default { | ||
toPrismaModel(input: EmailLog): PrismaRelationEmailLog { | ||
return { | ||
email_id: input.emailId as string, | ||
http_status: input.httpStatus, | ||
msg_id: input.msgId ?? null, | ||
to: input.to ?? null, | ||
tx_id: input.txId ?? null, | ||
created_at: input.createdAt ? new Date(input.createdAt) : null, | ||
created_by: input.createdBy as string, | ||
updated_at: input.updatedAt ? new Date(input.updatedAt) : null, | ||
updated_by: input.updatedBy as string | ||
}; | ||
}, | ||
|
||
fromPrismaModel(input: PrismaGraphEmailLog): EmailLog { | ||
return { | ||
emailId: input.email_id, | ||
httpStatus: Number(input.http_status), | ||
msgId: input.msg_id || '', | ||
to: input.to || '', | ||
createdAt: input.created_at?.toISOString() ?? null, | ||
createdBy: input.created_by, | ||
updatedAt: input.updated_at?.toISOString() ?? null, | ||
updatedBy: input.updated_by | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { IStamps } from '../interfaces/IStamps'; | ||
|
||
export type EmailLog = { | ||
emailId?: string; // Primary Key | ||
httpStatus: number; | ||
msgId?: string; | ||
to?: string; | ||
txId?: string; | ||
} & Partial<IStamps>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters