0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

chore(cloud): add payload to service log (#3712)

This commit is contained in:
Darcy Ye 2023-04-21 11:12:37 +08:00 committed by GitHub
parent dd24aafc17
commit b204cf828d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 7 deletions

View file

@ -16,6 +16,7 @@ import { RequestError } from '@withtyped/server';
import type { Queries } from '#src/queries/index.js';
import type { LogtoConnector } from '#src/utils/connector/index.js';
import { loadConnectorFactories } from '#src/utils/connector/index.js';
import { jsonObjectGuard } from '#src/utils/guard.js';
export const serviceCountLimitForTenant = 100;
@ -107,11 +108,12 @@ export class ServicesLibrary {
return sendMessage(data);
}
async addLog(tenantId: string, type: ServiceLogType) {
async addLog(tenantId: string, type: ServiceLogType, payload?: unknown) {
return this.queries.serviceLogs.insertLog({
id: generateStandardId(),
type,
tenantId,
payload: trySafe(() => jsonObjectGuard.parse(payload)),
});
}

View file

@ -1,14 +1,14 @@
import type { CreateServiceLog, ServiceLogType } from '@logto/schemas';
import type { PostgreSql } from '@withtyped/postgres';
import { sql } from '@withtyped/postgres';
import type { Queryable } from '@withtyped/server';
import type { JsonObject, Queryable } from '@withtyped/server';
import { insertInto } from '#src/utils/query.js';
export type ServiceLogsQueries = ReturnType<typeof createServiceLogsQueries>;
export const createServiceLogsQueries = (client: Queryable<PostgreSql>) => {
const insertLog = async (data: Omit<CreateServiceLog, 'payload'>) =>
const insertLog = async (data: Omit<CreateServiceLog, 'payload'> & { payload?: JsonObject }) =>
client.query(insertInto(data, 'service_logs'));
const countTenantLogs = async (tenantId: string, type: ServiceLogType) => {

View file

@ -65,7 +65,9 @@ describe('POST /api/services/send-email', () => {
async ({ status }) => {
expect(status).toBe(201);
expect(library.sendMessage).toBeCalledWith(ConnectorType.Email, mockSendMessagePayload);
expect(library.addLog).toBeCalledWith('tenantId', ServiceLogType.SendEmail);
expect(library.addLog).toBeCalledWith('tenantId', ServiceLogType.SendEmail, {
data: mockSendMessagePayload,
});
},
createHttpContext()
);
@ -125,7 +127,9 @@ describe('POST /api/services/send-sms', () => {
async ({ status }) => {
expect(status).toBe(201);
expect(library.sendMessage).toBeCalledWith(ConnectorType.Sms, mockSendMessagePayload);
expect(library.addLog).toBeCalledWith('tenantId', ServiceLogType.SendSms);
expect(library.addLog).toBeCalledWith('tenantId', ServiceLogType.SendSms, {
data: mockSendMessagePayload,
});
},
createHttpContext()
);

View file

@ -29,7 +29,7 @@ export const servicesRoutes = (library: ServicesLibrary) =>
}
await library.sendMessage(ConnectorType.Email, context.guarded.body.data);
await library.addLog(tenantId, ServiceLogType.SendEmail);
await library.addLog(tenantId, ServiceLogType.SendEmail, context.guarded.body);
return next({ ...context, status: 201 });
}
@ -55,7 +55,7 @@ export const servicesRoutes = (library: ServicesLibrary) =>
}
await library.sendMessage(ConnectorType.Sms, context.guarded.body.data);
await library.addLog(tenantId, ServiceLogType.SendSms);
await library.addLog(tenantId, ServiceLogType.SendSms, context.guarded.body);
return next({ ...context, status: 201 });
}

View file

@ -0,0 +1,13 @@
import type { Json } from '@withtyped/server';
import { z } from 'zod';
/**
* `jsonGuard` copied from https://github.com/colinhacks/zod#json-type.
* Can be moved to @logto/shared if needed.
*/
const jsonGuard: z.ZodType<Json> = z.lazy(() =>
z.union([z.number(), z.boolean(), z.string(), z.null(), z.array(jsonGuard), z.record(jsonGuard)])
);
export const jsonObjectGuard = z.record(jsonGuard);