2021-02-16 - Translations - Functions
Revision as of 14:18, 3 March 2021 by Sven the Barbarian (talk | contribs) (→CreateTranslationVertex)
Lambda Functions
GetTranslation
/**
* Finds a matching translation from an ordered list of language preferences, or return the defaultTranslatesTo translation
* @param {string} subjectVertexId
* @param {string} translationLabel
* @param {string[]} languageCodes - is an array of languageCodes, ordered by preference
*
* @returns {string} translation
*/
HdrApi
Validator: (standard)
handler logic
(standard)
logic
- const graphServiceName = {from Config table: TranslationsGraphServiceName}
- const foundTranslation = null
- for each languageCodes
- let translatesToEdgeId = #createTranslatesToEdgeId
- subjectVertexId
- translationLabel
- languageCode
- let translateVertex = invoke NPM module - izara-shared#neptuneGraphSharedLib.getObjectVertexByEdgeId
- edgeId: translatesToEdgeId
- if translateVertex != null
- foundTranslation = translateVertex.translation
- break
- let translatesToEdgeId = #createTranslatesToEdgeId
- if foundTranslation == null (no translations found for array of languageCodes
- let defaultTranslatesToEdgeId = #createDefaultTranslatesToEdgeId
- subjectVertexId
- translationLabel
- let translateVertex = invoke NPM module - izara-shared#neptuneGraphSharedLib.getObjectVertexByEdgeId
- edgeId: defaultTranslatesToEdgeId
- if translateVertex != null
- foundTranslation = translateVertex.translation
- else
- foundTranslation = (or could use a filler string - should never happen)
- let defaultTranslatesToEdgeId = #createDefaultTranslatesToEdgeId
- return foundTranslation
ListTranslationsOneLanguage
/**
* Finds all possible translations for one subject and language
* @param {string} subjectVertexId
* @param {string[]} languageCode
*
* @returns {object[]} array of translation objects (maybe edges as well)
*/
HdrApi
Validator: (standard)
handler logic
(standard)
logic
- const graphServiceName = {from Config table: TranslationsGraphServiceName}
- const translations = NPM module - izara-shared#neptuneGraphSharedLib.getVertexOutEdgesAndVertices
- graphServiceName: graphServiceName
- subjectVertexId: subjectVertexId
- edgeLabel: 'possiblyTranslatesTo'
- edgeProperties: {languageCode: languageCode}
- return translations
CreateSubjectVertex
/**
* Create a new vertex for the subject of translations
* @param {string} vertexId
* @param {string} vertexLabel
* @param {object} [vertexProperties]
*
*/
HdrSqs
Triggered from MsgIn queue, msgTag: "CreateSubjectVertex"
Validator: (standard)
handler logic
(standard)
logic
- const graphServiceName = {from Config table: TranslationsGraphServiceName}
- publish to graphServiceName MsgIn queue (msgTag: 'CreateVertex'):
- vertexId: vertexId
- vertexLabel: vertexLabel
- vertexProperties: vertexProperties
CreateTranslationVertex
/**
* Create a new vertex for a translation
* @param {string} vertexId
* @param {string} vertexLabel
* @param {string} translation
*
*/
HdrSqs
Triggered from MsgIn queue, msgTag: "CreateTranslationVertex"
Validator: (standard)
handler logic
(standard)
logic
- create vertex by publish message to Service - Graph Handler MsgIn queue (msgTag: 'CreateVertex'):
- vertexId: vertexId
- vertexLabel: vertexLabel
- vertexProperties: {translation: translation}
CreateTranslation
/**
* Create a new translation for a source object
* @param {string} subjectVertexId
* @param {string} translationLabel
* @param {string} languageCode
* @param {string} translation
* @param {string} userId
*
* @returns {Object[]} ?translation vertex id?
*/
HdrSqs
Triggered from MsgIn queue, msgTag: "CreateTranslation"
Validator: (standard)
handler logic
(standard)
HdrApi
Validator: (standard)
Authorizer
AppLevel
handler logic
(standard)
logic
Expects subject vertex to already exist
- const graphServiceName = {from Config table: TranslationsGraphServiceName}
- const translationVertexId = NPM module - izara-shared#neptuneGraphSharedLib.createVertexId
- vertexTag: 'translation'
- uniqueId: {random uuid}
- const existingVertices = NPM module - izara-shared#neptuneGraphSharedLib.getVertexOutEdgesAndVertices
- subjectId: subjectVertexId
- edgeLabel: 'possiblyTranslatesTo'
- let hasExistingSameLanguage = false
- iterate existingVertices
- if any match this languageCode and translation can return, no additional work to be done
- if any match this languageCode set hasExistingSameLanguage = true
- publish message to own MsgIn queue (msgTag: 'CreateTranslationVertex'):
- vertexId: translationVertexId
- vertexLabel: translationLabel + 'Translation'
- translation: translation
- create edgeId for possiblyTranslatesTo edge:
- const possiblyTranslatesToEdgeId = subjectVertexId + '_Possible_' + translationLabel + '_' + languageCode + '_' + translationVertexId
- create possiblyTranslatesTo edge by publish message to Service - Graph Handler MsgIn queue (msgTag: 'CreateEdge'):
- subjectId: subjectVertexId
- edgeLabel: 'possiblyTranslatesTo'
- edgeId: possiblyTranslatesToEdgeId
- objectId: translationVertexId
- edgeProperties:
- languageCode: languageCode
- if hasExistingSameLanguage == false
- const translatesToEdgeId = #createTranslatesToEdgeId
- subjectVertexId
- translationLabel
- languageCode
- create translatesTo edge by publish message to Service - Graph Handler MsgIn queue (msgTag: 'CreateEdge'):
- subjectId: subjectVertexId
- edgeLabel: 'translatesTo'
- edgeId: translatesToEdgeId
- objectId: translationVertexId
- edgeProperties:
- languageCode: languageCode
- const translatesToEdgeId = #createTranslatesToEdgeId
- if existingVertices.length == 0 (no existing possible translations)
- const defaultTranslatesToEdgeId = #createDefaultTranslatesToEdgeId
- subjectVertexId
- translationLabel
- create defaultTranslatesTo edge by publish message to Service - Graph Handler MsgIn queue (msgTag: 'CreateEdge'):
- subjectId: subjectVertexId
- edgeLabel: 'defaultTranslatesTo'
- edgeId: defaultTranslatesToEdgeId
- objectId: translationVertexId
- const defaultTranslatesToEdgeId = #createDefaultTranslatesToEdgeId
- const userVertexId = NPM module - izara-shared#neptuneGraphSharedLib.createVertexId
- vertexTag: 'user'
- uniqueId: userId
- const createdByEdgeId = NPM module - izara-shared#neptuneGraphSharedLib.createCreatedByEdgeId
- vertexId: translationVertexId
- create createdBy edge by publish message to Service - Graph Handler MsgIn queue (msgTag: 'CreateEdge'):
- subjectId: translationVertexId
- edgeLabel: 'createdBy'
- edgeId: createdByEdgeId
- objectId: userVertexId
- edgeProperties:
- timestamp: {current timestamp}
Functions
createTranslatesToEdgeId
/**
* @param {string} subjectVertexId
* @param {string} translationLabel
* @param {string} languageCode
*
* @returns {string} translatesToEdgeId or null if none found
*/
logic
- return subjectVertexId + '_' + translationLabel + '_' + languageCode
createDefaultTranslatesToEdgeId
/**
* @param {string} subjectVertexId
* @param {string} translationLabel
*
* @returns {string} defaultTranslatesToEdgeId or null if none found
*/
logic
- return subjectVertexId + '_Default_' + translationLabel