Skip to content

Commit

Permalink
feat(persist): Add support for activityLogId to get records endpoint (#…
Browse files Browse the repository at this point in the history
…3476)

<!-- Describe the problem and your solution --> 
Realized it's ideal to be able to log from persist consistent with what
we do on the other endpoints for saving records.

<!-- Issue ticket number and link (if applicable) -->

<!-- Testing instructions (skip if just adding/editing providers) -->

---------

Co-authored-by: Samuel Bodin <1637651+bodinsamuel@users.noreply.github.com>
  • Loading branch information
nalanj and bodinsamuel authored Feb 7, 2025
1 parent b35d738 commit 52bac25
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { ApiError, Endpoint, GetRecordsSuccess } from '@nangohq/types';
import type { EndpointRequest, EndpointResponse, RouteHandler, Route } from '@nangohq/utils';
import { records } from '@nangohq/records';
import { validateGetRecords } from './validate.js';
import type { LogContextStateless } from '@nangohq/logs';
import { logContextGetter } from '@nangohq/logs';

type GetRecords = Endpoint<{
Method: typeof method;
Expand All @@ -13,8 +15,9 @@ type GetRecords = Endpoint<{
Querystring: {
model: string;
externalIds: string[];
cursor: string;
cursor: string | undefined;
limit: number;
activityLogId: string | undefined;
};
Error: ApiError<'get_records_failed'>;
Success: GetRecordsSuccess;
Expand All @@ -28,17 +31,28 @@ const validate = validateGetRecords<GetRecords>();
const handler = async (req: EndpointRequest<GetRecords>, res: EndpointResponse<GetRecords>) => {
const {
params: { nangoConnectionId },
query: { model, externalIds, cursor }
query: { model, externalIds, cursor, limit, activityLogId }
} = req;

let logCtx: LogContextStateless | undefined = undefined;
if (activityLogId) {
logCtx = logContextGetter.getStateLess({ id: String(activityLogId) });
}

const result = await records.getRecords({
connectionId: nangoConnectionId,
model,
cursor,
externalIds,
limit: 100
limit
});

if (result.isOk()) {
await logCtx?.info(`Successfully found ${result.value.records.length} records`, {
model,
externalIds,
limit
});
res.json(result.value);
} else {
res.status(500).json({ error: { code: 'get_records_failed', message: `Failed to get records: ${result.error.message}` } });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const validateGetRecords = <E extends Endpoint<any>>() =>
model: z.string(),
cursor: z.string().optional(),
limit: z.coerce.number().int().positive().default(100),
activityLogId: z.string().optional(),
externalIds: z
.union([
z.string().min(1).max(256), // There is no diff between a normal query param and an array with one item
Expand Down

0 comments on commit 52bac25

Please # to comment.