-
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.
- Loading branch information
Showing
26 changed files
with
421 additions
and
11 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,5 @@ | ||
<mj-section> | ||
<mj-column> | ||
<mj-text>{{{text}}}</mj-text> | ||
</mj-column> | ||
</mj-section> |
16 changes: 16 additions & 0 deletions
16
prisma/migrations/20230423194845_add_logging/migration.sql
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,16 @@ | ||
-- CreateTable | ||
CREATE TABLE `Log` ( | ||
`id` VARCHAR(191) NOT NULL, | ||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), | ||
`action` VARCHAR(191) NOT NULL DEFAULT 'misc', | ||
`level` INTEGER NOT NULL, | ||
`message` LONGTEXT NOT NULL, | ||
`ip` VARCHAR(191) NOT NULL, | ||
`userId` VARCHAR(191) NULL, | ||
|
||
UNIQUE INDEX `Log_id_key`(`id`), | ||
PRIMARY KEY (`id`) | ||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE `Log` ADD CONSTRAINT `Log_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON DELETE SET NULL ON UPDATE CASCADE; |
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,121 @@ | ||
import type { User } from '@prisma/client'; | ||
import { prisma } from './prisma'; | ||
import { sendMail } from './mail'; | ||
export type LogAction = | ||
| 'send_mail' | ||
| 'submit_appartment' | ||
| 'submit_appartment_edit' | ||
| 'approve_appartment_edit' | ||
| 'report_appartment' | ||
| 'create_account' | ||
| 'login' | ||
| 'logout' | ||
| 'delete_account' | ||
| 'request_password_reset' | ||
| 'use_password_reset' | ||
| 'request_email_validation' | ||
| 'use_email_validation' | ||
| 'approve_appartent' | ||
| 'change_email' | ||
| 'change_password' | ||
| 'archive_appartment' | ||
| 'unarchive_appartment' | ||
| 'delete_appartment' | ||
| 'edit_account' | ||
| 'delete_appartment_edit' | ||
| 'delete_appartment_report' | ||
| 'generate_travel_times' | ||
| 'misc'; | ||
export const LOG_LEVELS = ['trace', 'info', 'warn', 'error', 'fatal'] as const; | ||
export type LogLevel = (typeof LOG_LEVELS)[number]; | ||
|
||
function serialize(o: any): string { | ||
if (typeof o === 'string') { | ||
return o; | ||
} | ||
if (o instanceof Error) { | ||
return o.message + (o.stack ? '\n' + o.stack : '(no stack trace available)'); | ||
} | ||
try { | ||
return JSON.stringify(o); | ||
} catch (error) { | ||
return o.toString(); | ||
} | ||
} | ||
|
||
async function _log( | ||
level: LogLevel, | ||
action: LogAction, | ||
by: User | string | null, | ||
...message: any[] | ||
) { | ||
let messageString = message.map(serialize).join(' '); | ||
const consoleArgs = [ | ||
level, | ||
typeof by === 'string' ? by : by?.email || 'unknown_user', | ||
action, | ||
messageString | ||
]; | ||
if (level === 'error' || 'fatal') { | ||
console.error(...consoleArgs); | ||
} else if (level === 'warn') { | ||
console.warn(...consoleArgs); | ||
} else { | ||
console.info(...consoleArgs); | ||
} | ||
let user = null; | ||
if (by) { | ||
user = await prisma.user.findFirst({ | ||
where: | ||
typeof by === 'string' ? { [by.includes('@') ? 'email' : 'id']: by } : { id: by.id } | ||
}); | ||
} | ||
const createdLog = await prisma.log.create({ | ||
data: { | ||
level: LOG_LEVELS.indexOf(level), | ||
action, | ||
message: messageString, | ||
ip: '', // TODO | ||
userId: user?.id ?? null | ||
} | ||
}); | ||
if (level === 'fatal') { | ||
const gods = await prisma.user.findMany({ | ||
where: { | ||
god: true | ||
} | ||
}); | ||
await Promise.all( | ||
gods.map((god) => | ||
sendMail({ | ||
to: god.email, | ||
subject: `Loca7: Fatal error when ${action}`, | ||
template: 'plain', | ||
data: { | ||
text: `At ${new Date().toISOString()}<br><strong>${ | ||
user?.email ?? 'unknown user' | ||
}</strong> triggered a fatal error when <strong>${action}</strong>:<br><br>${messageString.replaceAll( | ||
'\n', | ||
'<br>' | ||
)}<br><br><br><br>For some context: <a href="${process.env.ORIGIN || 'http://localhost:5173'}/logs#${createdLog.createdAt.toISOString()}" target="_blank">see logs</a>.` | ||
} | ||
}) | ||
) | ||
); | ||
} | ||
} | ||
|
||
type LogFunction = ( | ||
action: LogAction, | ||
by: User | string | null, | ||
...message: any[] | ||
) => Promise<void>; | ||
|
||
export const log = Object.fromEntries( | ||
LOG_LEVELS.map((level) => [ | ||
level.toLowerCase(), | ||
((action, by, ...message) => _log(level, action, by, ...message)) as LogFunction | ||
]) | ||
) as { | ||
[level in LogLevel]: LogFunction; | ||
}; |
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ import mjml2html from 'mjml'; | |
import nodemailer from 'nodemailer'; | ||
import path from 'path'; | ||
import { valueOfBooleanString } from './utils'; | ||
import { log } from './logging'; | ||
|
||
// generate:EmailTemplates | ||
/** | ||
|
@@ -15,6 +16,7 @@ export type EmailTemplates = | |
| 'announcement' | ||
| 'email-changed' | ||
| 'password-changed' | ||
| 'plain' | ||
| 'reset-password' | ||
| 'validate-email'; | ||
|
||
|
@@ -34,7 +36,7 @@ export const mailer = nodemailer.createTransport({ | |
: {}) | ||
}); | ||
|
||
export function sendMail({ | ||
export async function sendMail({ | ||
template, | ||
to, | ||
subject, | ||
|
@@ -45,12 +47,13 @@ export function sendMail({ | |
subject: string; | ||
data: Record<string, string>; | ||
}) { | ||
await log.info('send_mail', null, { to, template, subject, data }); | ||
const computedSubject = Handlebars.compile(subject)(data); | ||
const layout = readFileSync('mail-templates/_layout.mjml').toString('utf-8'); | ||
return mailer.sendMail({ | ||
from: '[email protected]', | ||
to, | ||
subject: computedSubject + ' @ ' + new Date().toISOString(), | ||
subject: computedSubject, | ||
html: mjml2html( | ||
Handlebars.compile( | ||
layout.replace( | ||
|
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
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
Oops, something went wrong.