2021-02-16 - Translations - Functions
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#graphSharedLib.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#graphSharedLib.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#graphSharedLib.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
CreateTranslation
/**
* Create a new translation for a source object
* @param {string} subjectVertexId
* @param {string} translationLabel
* @param {string} languageCode
* @param {string} translation
* @param {string} createdByUserId
* @param {object} [createdByEdgeProperties={}]
*
* @returns {string} translationVertexId
*/
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}
- // check existing translations match this language and/or translation
- const existingVertices = NPM module - izara-shared#graphSharedLib.getVertexOutEdgesAndVertices
- graphServiceName: graphServiceName
- 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
// create this translations vertex
- const translationId = {random uuid}
// create translationVertexId for additional edges if needed, also gets created separately in CreateChildVertexAndEdge Lambda
- const translationVertexId = #createTranslationVertexId
- translationId: translationId
- // no existing translations match this language and translation, so create translation vertex
- publish message to graphServiceName MsgIn queue (msgTag: 'CreateChildVertexAndEdge'):
- subjectVertexId: subjectVertexId
- vertexLabel: izara-market-shared#TranslationsLib.translationGraphTag
- uniqueId: translationId
- createdByUserId: createdByUserId
- edgeLabel: 'possiblyTranslatesTo'
- edgeUniqueSubId: translationLabel + '_' + languageCode
- vertexProperties: {translation: translation}
- edgeProperties: {languageCode: languageCode}
- createdByEdgeProperties: createdByEdgeProperties
- // if there are no other translations for this language, this translation becomes the current translatesTo
- 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
CreateNewTranslationSubjectAndTranslation
/**
* Create a new translation subject and an intitial translation
* @param {string} subjectUniqueId
* @param {string} subjectVertexLabel
* @param {object} [subjectVertexProperties={}]
* @param {string} languageCode
* @param {string} translation
* @param {string} createdByUserId
* @param {object} [createdByEdgeProperties={}]
*
* @returns {string} translationVertexId
*/
HdrSqs
Triggered from MsgIn queue, msgTag: "CreateNewTranslationSubjectAndTranslation"
Validator: (standard)
handler logic
(standard)
HdrApi
Validator: (standard)
Authorizer
AppLevel
handler logic
(standard)
logic
- const graphServiceName = {from Config table: TranslationsGraphServiceName}
- const subjectVertexId = NPM module - izara-shared#graphSharedLib.createVertexId
- vertexTag: subjectVertexLabel
- uniqueId: subjectUniqueId
- publish to graphServiceName MsgIn queue (msgTag: 'CreateVertex'):
- vertexId: subjectVertexId
- vertexLabel: subjectVertexLabel
- vertexProperties: subjectVertexProperties
// create this translations vertex
- const translationId = {random uuid}
// create translationVertexId, also gets created separately in CreateChildVertexAndEdge Lambda
- const translationVertexId = #createTranslationVertexId
- translationId: translationId
- // create translation vertex
- publish message to graphServiceName MsgIn queue (msgTag: 'CreateChildVertexAndEdge'):
- subjectVertexId: subjectVertexId
- vertexLabel: izara-market-shared#TranslationsLib.translationGraphTag
- uniqueId: translationId
- createdByUserId: createdByUserId
- edgeLabel: 'possiblyTranslatesTo'
- edgeUniqueSubId: subjectVertexLabel + '_' + languageCode
- vertexProperties: {translation: translation}
- edgeProperties: {languageCode: languageCode}
- createdByEdgeProperties: createdByEdgeProperties
- // this translation set to current translatesTo
- const translatesToEdgeId = #createTranslatesToEdgeId
- subjectVertexId
- subjectVertexLabel
- 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
- // this translation set to defaultTranslatesTo
- const defaultTranslatesToEdgeId = #createDefaultTranslatesToEdgeId
- subjectVertexId
- subjectVertexLabel
- create defaultTranslatesTo edge by publish message to Service - Graph Handler MsgIn queue (msgTag: 'CreateEdge'):
- subjectId: subjectVertexId
- edgeLabel: 'defaultTranslatesTo'
- edgeId: defaultTranslatesToEdgeId
- objectId: translationVertexId
Functions
createTranslationVertexId
/**
* @param {string} translationId
*
* @returns {string} translatesToEdgeId or null if none found
*/
logic
return = NPM module - izara-shared#graphSharedLib.createVertexId
- vertexTag: izara-market-shared#TranslationsLib.translationGraphTag
- uniqueId: translationId
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