MED-198: send notification if new responses
MED-198: send notification if new responses
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
import { createUserAnalysesApi } from '@/packages/features/user-analyses/src/server/api';
|
||||
import { getSupabaseServerClient } from '@/packages/supabase/src/clients/server-client';
|
||||
|
||||
import { readPrivateMessageResponse } from '~/lib/services/medipost/medipostPrivateMessage.service';
|
||||
|
||||
type ProcessedMessage = {
|
||||
@@ -16,6 +19,8 @@ type GroupedResults = {
|
||||
|
||||
export default async function syncAnalysisResults() {
|
||||
console.info('Syncing analysis results');
|
||||
const supabase = getSupabaseServerClient();
|
||||
const api = createUserAnalysesApi(supabase);
|
||||
|
||||
const processedMessages: ProcessedMessage[] = [];
|
||||
const excludedMessageIds: string[] = [];
|
||||
@@ -25,6 +30,12 @@ export default async function syncAnalysisResults() {
|
||||
processedMessages.push(result as ProcessedMessage);
|
||||
}
|
||||
|
||||
await api.sendAnalysisResultsNotification({
|
||||
hasFullAnalysisResponse: result.hasFullAnalysisResponse,
|
||||
hasPartialAnalysisResponse: result.hasAnalysisResponse,
|
||||
analysisOrderId: result.analysisOrderId,
|
||||
});
|
||||
|
||||
if (!result.messageId) {
|
||||
console.info('No more messages to process');
|
||||
break;
|
||||
|
||||
@@ -3,9 +3,7 @@
|
||||
import type { PostgrestError } from '@supabase/supabase-js';
|
||||
|
||||
import { GetMessageListResponse, MedipostAction } from '@/lib/types/medipost';
|
||||
import { createNotificationsApi } from '@/packages/features/notifications/src/server/api';
|
||||
import { createUserAnalysesApi } from '@/packages/features/user-analyses/src/server/api';
|
||||
import { pathsConfig } from '@/packages/shared/src/config';
|
||||
import { AnalysisOrderStatus } from '@/packages/shared/src/types/medipost-analysis';
|
||||
import type {
|
||||
MedipostOrderResponse,
|
||||
@@ -18,7 +16,6 @@ import axios from 'axios';
|
||||
import { toArray } from '@kit/shared/utils';
|
||||
import { Tables } from '@kit/supabase/database';
|
||||
|
||||
import { createI18nServerInstance } from '~/lib/i18n/i18n.server';
|
||||
import type { AnalysisResponseElement } from '~/lib/types/analysis-response-element';
|
||||
import type { AnalysisOrder } from '~/lib/types/order';
|
||||
|
||||
@@ -123,7 +120,7 @@ export async function canCreateAnalysisResponseElement({
|
||||
|
||||
if (existingAnalysisResponseElement.response_value && !responseValue) {
|
||||
log(
|
||||
`Analysis response element id=${analysisElementOriginalId} already exists for order with response value ${existingAnalysisResponseElement.response_value} but new response has no value`,
|
||||
`Analysis response element id=${analysisElementOriginalId} ${existingAnalysisResponseElement.response_value} but new response has no value`,
|
||||
);
|
||||
return false;
|
||||
}
|
||||
@@ -271,7 +268,6 @@ export async function syncPrivateMessage({
|
||||
order: Tables<{ schema: 'medreport' }, 'analysis_orders'>;
|
||||
}) {
|
||||
const supabase = getSupabaseServerAdminClient();
|
||||
const { t } = await createI18nServerInstance();
|
||||
|
||||
const orderStatus = AnalysisOrderStatus[TellimuseOlek];
|
||||
|
||||
@@ -304,7 +300,6 @@ export async function syncPrivateMessage({
|
||||
log,
|
||||
});
|
||||
|
||||
let newElementsAdded = 0;
|
||||
for (const element of newElements) {
|
||||
try {
|
||||
await upsertAnalysisResponseElement({
|
||||
@@ -313,7 +308,6 @@ export async function syncPrivateMessage({
|
||||
analysis_response_id: analysisResponseId,
|
||||
},
|
||||
});
|
||||
newElementsAdded++;
|
||||
} catch (e) {
|
||||
log(
|
||||
`Failed to create order response element for response id ${analysisResponseId}, element id '${element.analysis_element_original_id}' (order id: ${order.id})`,
|
||||
@@ -322,16 +316,6 @@ export async function syncPrivateMessage({
|
||||
}
|
||||
}
|
||||
|
||||
log(`Added ${newElementsAdded} new elements`);
|
||||
|
||||
if (newElementsAdded !== 0) {
|
||||
await createNotificationsApi(supabase).createNotification({
|
||||
account_id: analysisOrder.user_id,
|
||||
body: t('analysis-results:notification.body'),
|
||||
link: `${pathsConfig.app.analysisResults}/${order.id}`,
|
||||
});
|
||||
}
|
||||
|
||||
return (await hasAllAnalysisResponseElements({ analysisResponseId, order }))
|
||||
? { isCompleted: orderStatus === 'COMPLETED' }
|
||||
: { isPartial: true };
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
import { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
import { createNotificationsApi } from '@kit/notifications/api';
|
||||
import { pathsConfig } from '@kit/shared/config';
|
||||
import { getLogger } from '@kit/shared/logger';
|
||||
import type { UuringuVastus } from '@kit/shared/types/medipost-analysis';
|
||||
import { toArray } from '@kit/shared/utils';
|
||||
import { Database } from '@kit/supabase/database';
|
||||
|
||||
import { createI18nServerInstance } from '~/lib/i18n/i18n.server';
|
||||
|
||||
import type {
|
||||
AnalysisOrder,
|
||||
AnalysisOrderStatus,
|
||||
@@ -488,6 +492,39 @@ class UserAnalysesApi {
|
||||
})
|
||||
.throwOnError();
|
||||
}
|
||||
|
||||
async sendAnalysisResultsNotification({
|
||||
hasFullAnalysisResponse,
|
||||
hasPartialAnalysisResponse,
|
||||
analysisOrderId,
|
||||
}: {
|
||||
hasFullAnalysisResponse: boolean;
|
||||
hasPartialAnalysisResponse: boolean;
|
||||
analysisOrderId?: number;
|
||||
}) {
|
||||
if (!analysisOrderId) {
|
||||
return;
|
||||
}
|
||||
const { data, error: userError } = await this.client.auth.getUser();
|
||||
if (userError) {
|
||||
throw userError;
|
||||
}
|
||||
const { user } = data;
|
||||
const notificationsApi = createNotificationsApi(this.client);
|
||||
const { t } = await createI18nServerInstance();
|
||||
|
||||
console.info(
|
||||
`Order ${analysisOrderId} got new responses -> Sending new notification`,
|
||||
);
|
||||
|
||||
if (hasFullAnalysisResponse || hasPartialAnalysisResponse) {
|
||||
await notificationsApi.createNotification({
|
||||
account_id: user.id,
|
||||
body: t('analysis-results:notification.body'),
|
||||
link: `${pathsConfig.app.analysisResults}/${analysisOrderId}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function createUserAnalysesApi(client: SupabaseClient<Database>) {
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
{
|
||||
"extends": "@kit/tsconfig/base.json",
|
||||
"extends": "../../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json",
|
||||
"paths": {
|
||||
"~/lib/utils": ["../../../lib/utils.ts"]
|
||||
}
|
||||
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
|
||||
},
|
||||
"include": ["*.ts", "*.tsx", "src"],
|
||||
"exclude": ["node_modules"]
|
||||
|
||||
Reference in New Issue
Block a user