Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 36 additions & 27 deletions web/api/v2/minikit/send-notification/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type SendNotificationBodyV2 = yup.InferType<
typeof sendNotificationBodySchemaV2
>;

const WALLET_ADDRESS_BATCH_SIZE = 100;

export const logNotification = async (
serviceClient: GraphQLClient,
app_id: string,
Expand Down Expand Up @@ -77,12 +79,28 @@ export const logNotification = async (
return;
}

createNotificationLogSdk(serviceClient).CreateWalletAdressNotificationLogs({
objects: wallet_addresses.map((wallet_address) => ({
wallet_address,
notification_log_id: notificationLogId,
})),
});
const sdk = createNotificationLogSdk(serviceClient);

// Batch inserts to avoid oversized Hasura CTE queries
for (let i = 0; i < wallet_addresses.length; i += WALLET_ADDRESS_BATCH_SIZE) {
const batch = wallet_addresses.slice(i, i + WALLET_ADDRESS_BATCH_SIZE);

try {
await sdk.CreateWalletAdressNotificationLogs({
objects: batch.map((wallet_address) => ({
wallet_address,
notification_log_id: notificationLogId,
})),
});
} catch (error) {
logger.error("NotificationLog - failed to insert wallet address batch", {
app_id,
notificationLogId,
batchIndex: i,
error,
});
}
}
};

const getSchemaVersion = (body: object) => {
Expand Down Expand Up @@ -430,28 +448,19 @@ export const POST = async (req: NextRequest) => {
});
}
const response: SendNotificationResponse = data.result;
if (schemaVersion === "v1") {
logNotification(
serviceClient,
app_id,
wallet_addresses,
mini_app_path,
(parsedParams as SendNotificationBodyV1).message,
);
} else if (schemaVersion === "v2") {
const localisations = (parsedParams as SendNotificationBodyV2)
.localisations;
const logMessage =
schemaVersion === "v1"
? (parsedParams as SendNotificationBodyV1).message
: (parsedParams as SendNotificationBodyV2).localisations?.[0]?.message;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use deterministic locale for v2 log message

When schemaVersion is v2, the logged message is taken from localisations?.[0]?.message, which depends on client-supplied array order. This means the same notification payload can produce different notification_log.message values if localisations are reordered, and logs can end up in a non-English language even though validation guarantees an en localisation exists. After consolidating to one log row per send, pick a stable locale (e.g. en with fallback) so log data remains consistent and queryable.

Useful? React with 👍 / 👎.


for (const localisation of localisations) {
logNotification(
serviceClient,
app_id,
wallet_addresses,
mini_app_path,
localisation.message,
);
}
}
// Fire-and-forget: log wallet addresses once (not per localisation)
logNotification(
serviceClient,
app_id,
wallet_addresses,
mini_app_path,
logMessage,
);

return NextResponse.json({
success: true,
Expand Down
Loading