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:
parent
dd24aafc17
commit
b204cf828d
5 changed files with 26 additions and 7 deletions
|
@ -16,6 +16,7 @@ import { RequestError } from '@withtyped/server';
|
||||||
import type { Queries } from '#src/queries/index.js';
|
import type { Queries } from '#src/queries/index.js';
|
||||||
import type { LogtoConnector } from '#src/utils/connector/index.js';
|
import type { LogtoConnector } from '#src/utils/connector/index.js';
|
||||||
import { loadConnectorFactories } 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;
|
export const serviceCountLimitForTenant = 100;
|
||||||
|
|
||||||
|
@ -107,11 +108,12 @@ export class ServicesLibrary {
|
||||||
return sendMessage(data);
|
return sendMessage(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
async addLog(tenantId: string, type: ServiceLogType) {
|
async addLog(tenantId: string, type: ServiceLogType, payload?: unknown) {
|
||||||
return this.queries.serviceLogs.insertLog({
|
return this.queries.serviceLogs.insertLog({
|
||||||
id: generateStandardId(),
|
id: generateStandardId(),
|
||||||
type,
|
type,
|
||||||
tenantId,
|
tenantId,
|
||||||
|
payload: trySafe(() => jsonObjectGuard.parse(payload)),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
import type { CreateServiceLog, ServiceLogType } from '@logto/schemas';
|
import type { CreateServiceLog, ServiceLogType } from '@logto/schemas';
|
||||||
import type { PostgreSql } from '@withtyped/postgres';
|
import type { PostgreSql } from '@withtyped/postgres';
|
||||||
import { sql } 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';
|
import { insertInto } from '#src/utils/query.js';
|
||||||
|
|
||||||
export type ServiceLogsQueries = ReturnType<typeof createServiceLogsQueries>;
|
export type ServiceLogsQueries = ReturnType<typeof createServiceLogsQueries>;
|
||||||
|
|
||||||
export const createServiceLogsQueries = (client: Queryable<PostgreSql>) => {
|
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'));
|
client.query(insertInto(data, 'service_logs'));
|
||||||
|
|
||||||
const countTenantLogs = async (tenantId: string, type: ServiceLogType) => {
|
const countTenantLogs = async (tenantId: string, type: ServiceLogType) => {
|
||||||
|
|
|
@ -65,7 +65,9 @@ describe('POST /api/services/send-email', () => {
|
||||||
async ({ status }) => {
|
async ({ status }) => {
|
||||||
expect(status).toBe(201);
|
expect(status).toBe(201);
|
||||||
expect(library.sendMessage).toBeCalledWith(ConnectorType.Email, mockSendMessagePayload);
|
expect(library.sendMessage).toBeCalledWith(ConnectorType.Email, mockSendMessagePayload);
|
||||||
expect(library.addLog).toBeCalledWith('tenantId', ServiceLogType.SendEmail);
|
expect(library.addLog).toBeCalledWith('tenantId', ServiceLogType.SendEmail, {
|
||||||
|
data: mockSendMessagePayload,
|
||||||
|
});
|
||||||
},
|
},
|
||||||
createHttpContext()
|
createHttpContext()
|
||||||
);
|
);
|
||||||
|
@ -125,7 +127,9 @@ describe('POST /api/services/send-sms', () => {
|
||||||
async ({ status }) => {
|
async ({ status }) => {
|
||||||
expect(status).toBe(201);
|
expect(status).toBe(201);
|
||||||
expect(library.sendMessage).toBeCalledWith(ConnectorType.Sms, mockSendMessagePayload);
|
expect(library.sendMessage).toBeCalledWith(ConnectorType.Sms, mockSendMessagePayload);
|
||||||
expect(library.addLog).toBeCalledWith('tenantId', ServiceLogType.SendSms);
|
expect(library.addLog).toBeCalledWith('tenantId', ServiceLogType.SendSms, {
|
||||||
|
data: mockSendMessagePayload,
|
||||||
|
});
|
||||||
},
|
},
|
||||||
createHttpContext()
|
createHttpContext()
|
||||||
);
|
);
|
||||||
|
|
|
@ -29,7 +29,7 @@ export const servicesRoutes = (library: ServicesLibrary) =>
|
||||||
}
|
}
|
||||||
|
|
||||||
await library.sendMessage(ConnectorType.Email, context.guarded.body.data);
|
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 });
|
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.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 });
|
return next({ ...context, status: 201 });
|
||||||
}
|
}
|
||||||
|
|
13
packages/cloud/src/utils/guard.ts
Normal file
13
packages/cloud/src/utils/guard.ts
Normal 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);
|
Loading…
Reference in a new issue