Skip to content

Instantly share code, notes, and snippets.

@IsaacGonzalez
Created July 16, 2021 19:51
Show Gist options
  • Save IsaacGonzalez/ac423255e64b3b195efd401de7f06e62 to your computer and use it in GitHub Desktop.
Save IsaacGonzalez/ac423255e64b3b195efd401de7f06e62 to your computer and use it in GitHub Desktop.
private async databaseIdMapping(request: Request, response: Response): Promise<void> {
try {
const urn: string = Utils.makeSafeUrn(request.params.urn || '');
const guid: string = request.params.guid || (request.query.guid as string) || null;
const region: string = request.query.region as string || Forge.DerivativesApi.RegionEnum.US;
const reverse: boolean = (request.query.reverse as string) === 'true'; // defaults to false
const dbBuffers: Svf2PropertiesCache = await this.utils.get(urn, guid, region);
if (!dbBuffers)
return (response.status(404).end());
// const propsDb: Svf2Properties = new Svf2Properties(dbBuffers);
const dbidIdx: Uint32Array = new Uint32Array(dbBuffers.dbid.buffer, dbBuffers.dbid.byteOffset, dbBuffers.dbid.byteLength / Uint32Array.BYTES_PER_ELEMENT);
const sep: string = (request.query.sep as string) || ',';
let dbIds: number[] = Utils.csvToNumber(request.query.ids as string, sep); // csv format
if (!dbIds || isNaN(dbIds[0]))
dbIds = Array.from({ length: dbidIdx.length - 1 }, (_, i) => i + 1);
const idmapping: { [index: number]: number } = {};
if ( !reverse )
dbIds.map((id: number): any => idmapping[id] = dbidIdx[id]);
else
dbIds.map((id: number): any => idmapping[id] = dbidIdx.indexOf(id));
response.json({
data: {
mapping: idmapping,
type: reverse ? 'svf2->svf' : 'svf->svf2',
}
});
} catch (ex) {
console.error(ex.message || ex.statusMessage || `${ex.statusBody.code}: ${JSON.stringify(ex.statusBody.detail)}`);
response.status(ex.statusCode || 500).send(ex.message || ex.statusMessage || `${ex.statusBody.code}: ${JSON.stringify(ex.statusBody.detail)}`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment