2021-02-16 - Translations - Functions
Lambda Functions
Translation/Get
/**
* Finds a matching translatesTo from an ordered list of language preferences, or return the defaultTranslatesTo translation
* @param {string} subjectVertexTag
* @param {string} subjectVertexUniqueId
* @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 translationGraphTag = {from Config table: GraphTag / translation}
- const translatesToGraphTag = {from Config table: GraphTag / translatesTo}
- const defaultTranslatesToGraphTag = {from Config table: GraphTag / defaultTranslatesTo}
- const foundTranslation = null
- for each languageCodes
- let translatesToEdgeId = #createTranslatesToEdgeId
- subjectVertexTag: subjectVertexTag
- subjectVertexUniqueId: subjectVertexUniqueId
- languageCode: languageCode
- translatesToGraphTag: translatesToGraphTag
- let translateVertex = invoke NPM module - izara-shared#graphSharedLib.getObjectVertexByEdgeId
- graphServiceName: graphServiceName
- 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
- subjectVertexTag: subjectVertexTag
- subjectVertexUniqueId: subjectVertexUniqueId
- defaultTranslatesToGraphTag: defaultTranslatesToGraphTag
- let translateVertex = invoke NPM module - izara-shared#graphSharedLib.getObjectVertexByEdgeId
- graphServiceName: graphServiceName
- edgeId: defaultTranslatesToEdgeId
- if translateVertex != null
- foundTranslation = translateVertex.translation
- else
- foundTranslation = (or could use a filler string - should never happen)
- let defaultTranslatesToEdgeId = #createDefaultTranslatesToEdgeId
- return foundTranslation
Translation/ListOneLanguage
/**
* Finds all possible translations for one subject and language
* @param {string} subjectVertexTag
* @param {string} subjectVertexUniqueId
* @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 possiblyTranslatesToGraphTag = {from Config table: GraphTag / possiblyTranslatesTo}
- const translations = NPM module - izara-shared#graphSharedLib.getVertexOutEdgesAndVerticesByVertexTagAndVertexUniqueId
- graphServiceName: graphServiceName
- subjectVertexTag: subjectVertexTag
- subjectVertexUniqueId: subjectVertexUniqueId
- edgeLabel: possiblyTranslatesToGraphTag
- edgeProperties: {languageCode: languageCode}
- return translations
Translation/CreateSubject
/**
* Create a new vertex for the subject of translations
* @param {string} vertexTag
* @param {string} vertexUniqueId
* @param {object} [vertexProperties={}]
* @param {object} [createdByEdgeProperties={}]
*
*/
HdrSqs
Triggered from MsgIn queue, msgTag: "CreateTranslationSubject"
Validator: (standard)
handler logic
(standard)
logic
- const graphServiceName = {from Config table: TranslationsGraphServiceName}
- publish to graphServiceName MsgIn queue (msgTag: 'CreateVertex'):
- vertexTag: vertexTag
- vertexUniqueId: vertexUniqueId
- createdByUserId: ?_izContext.authorized/signedInUser..?
- vertexProperties: vertexProperties
- createdByEdgeProperties: createdByEdgeProperties
Translation/Create
/**
* Create a new translation for an existing subject vertex
* @param {string} subjectVertexTag
* @param {string} subjectVertexUniqueId
* @param {string} languageCode
* @param {string} translation
* @param {string} createdByUserId
* @param {object} [createdByEdgeProperties={}]
*
* @returns {string} translationId
*/
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 translationGraphTag = {from Config table: GraphTag / translation}
- const possiblyTranslatesToGraphTag = {from Config table: GraphTag / possiblyTranslatesTo}
- const translatesToGraphTag = {from Config table: GraphTag / translatesTo}
- const defaultTranslatesToGraphTag = {from Config table: GraphTag / defaultTranslatesTo}
- // check any existing translations match this language and/or translation
- const existingTranslations = NPM module - izara-shared#graphSharedLib.getVertexOutEdgesAndVerticesByVertexTagAndVertexUniqueId
- graphServiceName: graphServiceName
- subjectVertexTag: subjectVertexTag
- subjectVertexUniqueId: subjectVertexUniqueId
- edgeLabel: possiblyTranslatesToGraphTag
- let hasExistingSameLanguage = false
- iterate existingTranslations
- 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 translation vertex
- publish message to graphServiceName MsgIn queue (msgTag: 'CreateChildVertexAndEdge'):
- subjectVertexTag: subjectVertexTag
- subjectVertexUniqueId: subjectVertexUniqueId
- vertexTag: translationGraphTag
- vertexUniqueId: translationId
- createdByUserId: createdByUserId
- edgeLabel: possiblyTranslatesToGraphTag
- 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
- // this translation set to current translatesTo with special edgeId
- const translatesToEdgeId = #createTranslatesToEdgeId
- subjectVertexTag: subjectVertexTag
- subjectVertexUniqueId: subjectVertexUniqueId
- languageCode: languageCode
- translatesToGraphTag: translatesToGraphTag
- publish message to graphServiceName MsgIn queue (msgTag: 'CreateEdgeFromVertexTagsAndUniqueIds'):
- subjectVertexTag: subjectVertexTag
- subjectVertexUniqueId: subjectVertexUniqueId
- edgeLabel: translatesToGraphTag
- objectVertexTag: translationGraphTag
- objectVertexUniqueId: translationId
- edgeId: translatesToEdgeId
- edgeProperties: {languageCode: languageCode}
- // report the new translation
- send message to own MsgOut queue, msgTag: "TranslationCreated"
- subjectVertexTag: subjectVertexTag
- subjectVertexUniqueId: subjectVertexUniqueId
- translationId: translationId
- languageCode: languageCode
- translation: translation
- createdByUserId: createdByUserId
- return translationId
Translation/CreateNewSubjectAndTranslation
/**
* Create a new translation subject and an intitial translation
* @param {string} subjectVertexTag
* @param {string} subjectVertexUniqueId
* @param {object} [subjectVertexProperties={}]
* @param {string} languageCode
* @param {string} translation
* @param {string} createdByUserId
* @param {object} [createdByEdgeProperties={}]
*
* @returns {string} translationId
*/
HdrSqs
Triggered from MsgIn queue, msgTag: "CreateNewSubjectAndTranslation"
Validator: (standard)
handler logic
(standard)
HdrApi
Validator: (standard)
Authorizer
AppLevel
handler logic
(standard)
logic
- const graphServiceName = {from Config table: TranslationsGraphServiceName}
- const translationGraphTag = {from Config table: GraphTag / translation}
- const possiblyTranslatesToGraphTag = {from Config table: GraphTag / possiblyTranslatesTo}
- const translatesToGraphTag = {from Config table: GraphTag / translatesTo}
- const defaultTranslatesToGraphTag = {from Config table: GraphTag / defaultTranslatesTo}
- // create subject vertex
- publish to graphServiceName MsgIn queue (msgTag: 'CreateVertex'):
- vertexTag: subjectVertexTag
- vertexUniqueId: subjectVertexUniqueId
- createdByUserId: createdByUserId
- vertexProperties: subjectVertexProperties
- createdByEdgeProperties: createdByEdgeProperties
// create the random translation uniqueId
- const translationId = {random uuid}
- // create translation vertex
- publish message to graphServiceName MsgIn queue (msgTag: 'CreateChildVertexAndEdge'):
- subjectVertexTag: subjectVertexTag
- subjectVertexUniqueId: subjectVertexUniqueId
- vertexTag: translationGraphTag
- vertexUniqueId: translationId
- createdByUserId: createdByUserId
- edgeLabel: possiblyTranslatesToGraphTag
- vertexProperties: {translation: translation}
- edgeProperties: {languageCode: languageCode}
- createdByEdgeProperties: createdByEdgeProperties
- // this translation set to current translatesTo with special edgeId
- const translatesToEdgeId = #createTranslatesToEdgeId
- subjectVertexTag: subjectVertexTag
- subjectVertexUniqueId: subjectVertexUniqueId
- languageCode: languageCode
- translatesToGraphTag: translatesToGraphTag
- publish message to graphServiceName MsgIn queue (msgTag: 'CreateEdgeFromVertexTagsAndUniqueIds'):
- subjectVertexTag: subjectVertexTag
- subjectVertexUniqueId: subjectVertexUniqueId
- edgeLabel: translatesToGraphTag
- objectVertexTag: translationGraphTag
- objectVertexUniqueId: translationId
- edgeId: translatesToEdgeId
- edgeProperties: {languageCode: languageCode}
- // this translation set to defaultTranslatesTo with special edgeId
- const defaultTranslatesToEdgeId = #createDefaultTranslatesToEdgeId
- subjectVertexTag: subjectVertexTag
- subjectVertexUniqueId: subjectVertexUniqueId
- defaultTranslatesToGraphTag: defaultTranslatesToGraphTag
- publish message to graphServiceName MsgIn queue (msgTag: 'CreateEdgeFromVertexTagsAndUniqueIds'):
- subjectVertexTag: subjectVertexTag
- subjectVertexUniqueId: subjectVertexUniqueId
- edgeLabel: defaultTranslatesToGraphTag
- objectVertexTag: translationGraphTag
- objectVertexUniqueId: translationId
- edgeId: defaultTranslatesToEdgeId
- edgeProperties: {languageCode: languageCode}
- // report the new translation
- send message to own MsgOut queue, msgTag: "TranslationCreated"
- subjectVertexTag: subjectVertexTag
- subjectVertexUniqueId: subjectVertexUniqueId
- translationId: translationId
- languageCode: languageCode
- translation: translation
- createdByUserId: createdByUserId
- return translationId
Functions
createTranslatesToEdgeId
/**
* @param {string} subjectVertexTag
* @param {string} subjectVertexUniqueId
* @param {string} languageCode
* @param {string} translatesToGraphTag
*
* @returns {string} translatesToEdgeId
*/
logic
A subjectVertex can only ever have 1 translatesTo edge per language, so we can create a standardized edgeId that does not include the objectVertexId, this allows more efficient queries to find this edge and object vertex.
- const subjectVertexId = NPM module - izara-shared#graphSharedLib.createVertexId
- vertexTag = subjectVertexTag
- vertexUniqueId = subjectVertexUniqueId
- return subjectVertexId + '_' + translatesToGraphTag + '_' + languageCode
createDefaultTranslatesToEdgeId
/**
* @param {string} subjectVertexTag
* @param {string} subjectVertexUniqueId
* @param {string} defaultTranslatesToGraphTag
*
* @returns {string} defaultTranslatesToEdgeId
*/
logic
A subjectVertex can only ever have 1 defaultTranslatesTo edge, so we can create a standardized edgeId that does not include the objectVertexId, this allows more efficient queries to find this edge and object vertex.
- return subjectVertexId + '_' + defaultTranslatesToGraphTag + '_' + translationLabel