Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release to val #203

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 36 additions & 0 deletions src/libs/opensearch-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,40 @@ export async function getItem(host:string, index:string, id:string){
} catch(e) {
console.log({e})
}
}

export async function createIndexIfNotExists(host:string, index:string) {
client = client || (await getClient(host));
try {
const indexExists = await client.indices.exists({ index, });
if (!indexExists.body) {
const createResponse = await client.indices.create({
index,
});

console.log('Index created:', createResponse);
} else {
console.log('Index already exists.');
}
} catch (error) {
console.error('Error creating index:', error);
throw error;
}
}

export async function updateFieldMapping(host:string, index:string, properties: object) {
client = client || (await getClient(host));
try {
const response = await client.indices.putMapping({
index: index,
body: {
properties,
},
});

console.log('Field mapping updated:', response);
} catch (error) {
console.error('Error updating field mapping:', error);
throw error;
}
}
1 change: 0 additions & 1 deletion src/packages/shared-types/action-types/withdraw-record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ import { z } from "zod";
export const withdrawRecordSchema = z.object({
raiWithdrawEnabled: z.boolean(),
});

export type WithdrawRecord = z.infer<typeof withdrawRecordSchema>;
1 change: 1 addition & 0 deletions src/packages/shared-types/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export enum Action {
ENABLE_RAI_WITHDRAW = "enable-rai-withdraw",
DISABLE_RAI_WITHDRAW = "disable-rai-withdraw",
ISSUE_RAI = "issue-rai",
RESPOND_TO_RAI = "respond-to-rai",
}
204 changes: 140 additions & 64 deletions src/packages/shared-types/onemac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,88 @@ const onemacAttachmentSchema = z.object({
key: z.string().nullish(),
uploadDate: z.number().nullish(),
});
export type OnemacAttachmentSchema = z.infer<typeof onemacAttachmentSchema>;

export const raiSchema = z.object({
id: z.string(),
requestedDate: z.number(),
responseDate: z.number().nullish(),
withdrawnDate: z.number().nullish(),
attachments: z.array(onemacAttachmentSchema).nullish(),
additionalInformation: z.string().nullable().default(null),
submitterName: z.string(),
submitterEmail: z.string(),
});
export type RaiSchema = z.infer<typeof raiSchema>;

export interface RaiData {
[key: number]: {
requestedDate?: string;
responseDate?: string;
withdrawnDate?: string;
response?: {
additionalInformation: string;
submitterName: string | null;
submitterEmail: string | null;
attachments: any[] | null; // You might want to specify the type of attachments
};
request?: {
additionalInformation: string;
submitterName: string | null;
submitterEmail: string | null;
attachments: any[] | null; // You might want to specify the type of attachments
};
};
}

export const transformRaiIssue = (id: string) => {
return raiSchema.transform((data) => ({
id,
rais: {
[data.requestedDate]: {
request: {
attachments:
data.attachments?.map((attachment) => {
return handleAttachment(attachment);
}) ?? null,
additionalInformation: data.additionalInformation,
submitterName: data.submitterName,
submitterEmail: data.submitterEmail,
},
},
},
}));
};
export type RaiIssueTransform = z.infer<ReturnType<typeof transformRaiIssue>>;

export const transformRaiResponse = (id: string) => {
return raiSchema.transform((data) => ({
id,
rais: {
[data.requestedDate]: {
response: {
attachments:
data.attachments?.map((attachment) => {
return handleAttachment(attachment);
}) ?? null,
additionalInformation: data.additionalInformation,
submitterName: data.submitterName,
submitterEmail: data.submitterEmail,
},
},
},
}));
};
export type RaiResponseTransform = z.infer<
ReturnType<typeof transformRaiResponse>
>;

export const onemacSchema = z.object({
additionalInformation: z.string().nullable().default(null),
submitterName: z.string(),
submitterEmail: z.string(),
attachments: z.array(onemacAttachmentSchema).nullish(),
raiWithdrawEnabled: z.boolean().optional(),
raiWithdrawEnabled: z.boolean().default(false),
raiResponses: z
.array(
z.object({
Expand All @@ -30,75 +105,76 @@ export const onemacSchema = z.object({
});

export const transformOnemac = (id: string) => {
return onemacSchema.transform((data) => ({
id,
attachments:
data.attachments?.map((attachment) => {
// this is a legacy onemac attachment
let bucket = "";
let key = "";
let uploadDate = 0;
if ("bucket" in attachment) {
bucket = attachment.bucket as string;
}
if ("key" in attachment) {
key = attachment.key as string;
}
if ("uploadDate" in attachment) {
uploadDate = attachment.uploadDate as number;
}
if (bucket == "") {
const parsedUrl = s3ParseUrl(attachment.url || "");
if (!parsedUrl) return null;
bucket = parsedUrl.bucket;
key = parsedUrl.key;
uploadDate = parseInt(attachment.s3Key?.split("/")[0] || "0");
}

return {
title: attachment.title,
filename: attachment.filename,
uploadDate,
bucket,
key,
return onemacSchema.transform((data) => {
const transformedData = {
id,
attachments:
data.attachments?.map((attachment) => {
return handleAttachment(attachment);
}) ?? null,
raiWithdrawEnabled: data.raiWithdrawEnabled,
additionalInformation: data.additionalInformation,
submitterEmail: data.submitterEmail,
submitterName: data.submitterName === "-- --" ? null : data.submitterName,
origin: "oneMAC",
rais: {} as RaiData,
};
if (data.raiResponses) {
data.raiResponses.forEach((raiResponse, index) => {
// We create an rai keyed off the index, because we don't know which rai it was in response to. Best we can do.
transformedData["rais"][index] = {
responseDate: raiResponse.submissionTimestamp.toString(),
response: {
additionalInformation: raiResponse.additionalInformation || "",
submitterName: null,
submitterEmail: null,
attachments:
raiResponse.attachments?.map((attachment) => {
return handleAttachment(attachment);
}) ?? null,
},
};
}) ?? null,
raiWithdrawEnabled: data.raiWithdrawEnabled,
raiResponses:
data.raiResponses?.map((response) => {
return {
additionalInformation: response.additionalInformation,
submissionTimestamp: response.submissionTimestamp,
attachments:
response.attachments?.map((attachment) => {
const uploadDate = parseInt(
attachment.s3Key?.split("/")[0] || "0"
);
const parsedUrl = s3ParseUrl(attachment.url || "");
if (!parsedUrl) return null;
const { bucket, key } = parsedUrl;

return {
...attachment,
uploadDate,
bucket,
key,
};
}) ?? null,
};
}) ?? null,
additionalInformation: data.additionalInformation,
submitterEmail: data.submitterEmail,
submitterName: data.submitterName === "-- --" ? null : data.submitterName,
origin: "oneMAC",
}));
});
}
return transformedData;
});
};

export type OneMacSink = z.infer<typeof onemacSchema>;
export type OneMacTransform = z.infer<ReturnType<typeof transformOnemac>>;
export type OneMacRecordsToDelete = Omit<
{
[Property in keyof OneMacTransform]: undefined;
[Property in keyof OneMacTransform]: null;
},
"id"
"id" | "rais"
> & { id: string };

function handleAttachment(attachment: OnemacAttachmentSchema) {
let bucket = "";
let key = "";
let uploadDate = 0;
if ("bucket" in attachment) {
bucket = attachment.bucket as string;
}
if ("key" in attachment) {
key = attachment.key as string;
}
if ("uploadDate" in attachment) {
uploadDate = attachment.uploadDate as number;
}
if (bucket == "") {
const parsedUrl = s3ParseUrl(attachment.url || "");
if (!parsedUrl) return null;
bucket = parsedUrl.bucket;
key = parsedUrl.key;
uploadDate = parseInt(attachment.s3Key?.split("/")[0] || "0");
}

return {
title: attachment.title,
filename: attachment.filename,
uploadDate,
bucket,
key,
};
}
6 changes: 4 additions & 2 deletions src/packages/shared-types/opensearch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SeaToolTransform } from "./seatool";
import { OneMacTransform } from "./onemac";
import { OneMacTransform, RaiIssueTransform } from "./onemac";
import { Action } from "./actions";

export type OsHit<T> = {
Expand Down Expand Up @@ -32,7 +32,9 @@ export type OsResponse<T> = {
aggregations?: OsAggResult;
};

export type OsMainSourceItem = OneMacTransform & SeaToolTransform;
export type OsMainSourceItem = OneMacTransform &
SeaToolTransform &
RaiIssueTransform;
export type OsMainSearchResponse = OsResponse<OsMainSourceItem>;
export type SearchData = OsHits<OsMainSourceItem>;
export type ItemResult = OsHit<OsMainSourceItem> & {
Expand Down
Loading
Loading