-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(anni): resolve bricks & reorganize db helpers
- Loading branch information
1 parent
a52303c
commit 6a5e82a
Showing
15 changed files
with
143 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
annotation-interface/database/helpers/brick-translations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { SupportedLanguage } from '../../common/constants'; | ||
import { ITextBrickTranslation } from '../models/TextBrick'; | ||
import { OptionalId } from './types'; | ||
|
||
export function translationsToMap<IdT extends OptionalId = undefined>( | ||
translations: ITextBrickTranslation<IdT>[], | ||
): Map<SupportedLanguage, string> { | ||
const map = new Map<SupportedLanguage, string>(); | ||
if (translations) { | ||
translations.forEach((translation) => | ||
map.set(translation.language, translation.text), | ||
); | ||
} | ||
return map; | ||
} | ||
|
||
export function translationsToArray( | ||
translations: Map<SupportedLanguage, string>, | ||
): ITextBrickTranslation[] { | ||
return Array.from(translations.entries()).map(([language, text]) => { | ||
return { language, text }; | ||
}); | ||
} | ||
|
||
export function translationIsValid<IdT extends OptionalId = undefined>( | ||
translation: ITextBrickTranslation<IdT>, | ||
): boolean { | ||
return translation.text.length > 0; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { FilterQuery, Types } from 'mongoose'; | ||
|
||
import { pharMeLanguage, SupportedLanguage } from '../../common/constants'; | ||
import { | ||
ServerGuidelineOverview, | ||
ServerMedication, | ||
} from '../../common/server-types'; | ||
import { IGuidelineAnnotation } from '../models/GuidelineAnnotation'; | ||
import { IMedAnnotation } from '../models/MedAnnotation'; | ||
import TextBrick, { ITextBrick } from '../models/TextBrick'; | ||
import { translationsToMap } from './brick-translations'; | ||
import { MongooseId, OptionalId } from './types'; | ||
|
||
export const medicationBrickPlaceholders = ['drug-name'] as const; | ||
export const allBrickPlaceholders = [ | ||
...medicationBrickPlaceholders, | ||
'gene-symbol', | ||
'gene-result', | ||
] as const; | ||
type BrickPlaceholderValues = { | ||
[Property in typeof allBrickPlaceholders[number]]?: string; | ||
}; | ||
|
||
type BrickResolver = | ||
| { from: 'medAnnotation'; with: IMedAnnotation<MongooseId> } | ||
| { from: 'serverMedication'; with: ServerMedication } | ||
| { from: 'guidelineAnnotation'; with: IGuidelineAnnotation<MongooseId> } | ||
| { from: 'serverGuideline'; with: ServerGuidelineOverview }; | ||
|
||
const getPlaceholders = ({ | ||
from: type, | ||
with: resolver, | ||
}: BrickResolver): BrickPlaceholderValues => { | ||
switch (type) { | ||
case 'medAnnotation': | ||
return { 'drug-name': resolver.medicationName }; | ||
case 'serverMedication': | ||
return { 'drug-name': resolver.name }; | ||
case 'guidelineAnnotation': | ||
return { | ||
'drug-name': resolver.medicationName, | ||
'gene-symbol': resolver.geneSymbol, | ||
'gene-result': resolver.geneResult, | ||
}; | ||
case 'serverGuideline': | ||
return { | ||
'drug-name': resolver.medication.name, | ||
'gene-symbol': resolver.phenotype.geneSymbol.name, | ||
'gene-result': resolver.phenotype.geneResult.name, | ||
}; | ||
} | ||
}; | ||
|
||
export type ResolvedBrick<IdT extends OptionalId> = [ | ||
_id: IdT, | ||
text: string | undefined, | ||
]; | ||
|
||
export function resolveBricks<IdT extends OptionalId>( | ||
resolver: BrickResolver, | ||
bricks: ITextBrick<IdT>[], | ||
language: SupportedLanguage = pharMeLanguage, | ||
): ResolvedBrick<IdT>[] { | ||
const placeholders = getPlaceholders(resolver); | ||
const resolved = bricks.map(({ _id, translations }) => { | ||
let text = translationsToMap(translations).get(language); | ||
if (text) { | ||
Object.entries(placeholders).forEach(([placeholder, replace]) => { | ||
text = text!.replaceAll(`#${placeholder}`, replace); | ||
}); | ||
} | ||
return [_id, text] as ResolvedBrick<IdT>; | ||
}); | ||
|
||
return resolved; | ||
} | ||
|
||
export const findResolvedBricks = async ( | ||
resolver: BrickResolver, | ||
filter: FilterQuery<ITextBrick<MongooseId>>, | ||
language: SupportedLanguage = pharMeLanguage, | ||
): Promise<ResolvedBrick<Types.ObjectId>[]> => { | ||
const bricks = await TextBrick!.find(filter).lean().exec(); | ||
return resolveBricks(resolver, bricks, language); | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters