import { Vocabulary , Definition } from "@ulangi/ulangi-common/interfaces" ; import { DefinitionStatus } from '@ulangi/ulangi-common/enums' ; export function prepareValuesForInsert ( valueByName ) { const vocabulary = { vocabularyId : valueByName . vocabularyId , vocabularyText : valueByName . vocabularyText , vocabularyStatus : valueByName . vocabularyStatus , updatedAt : new Date ( ) , updatedStatusAt : new Date ( ) , createdAt : new Date ( ) , firstSyncedAt : null , lastSyncedAt : null , definitions : valueByName . definitions . split ( "\n---\n" ) . filter ( ( meaning ) : boolean => { return meaning . trim ( ) !== "" ; } ) . map ( ( meaning ) : Definition => { return { definitionId : Utilities . getUuid ( ) , definitionStatus : "ACTIVE" as DefinitionStatus , source : "user" , meaning : meaning . trim ( ) , wordClasses : [ ] , createdAt : new Date ( ) , updatedAt : new Date ( ) , updatedStatusAt : new Date ( ) , firstSyncedAt : null , lastSyncedAt : null , extraData : [ ] } ; } ) , category : { categoryName : valueByName . category , createdAt : new Date ( ) , updatedAt : new Date ( ) , firstSyncedAt : null , lastSyncedAt : null , } , level : valueByName . spacedRepetitionLevel , lastLearnedAt : null , writing : { level : valueByName . writingLevel , disabled : valueByName . disableWriting , lastWrittenAt : null , createdAt : new Date ( ) , updatedAt : new Date ( ) , firstSyncedAt : null , lastSyncedAt : null , } , extraData : [ ] } ; if ( vocabulary . vocabularyText === "" ) { throw `Empty vocabularyText is not allowed (check the row with vocabularyId ${ vocabulary . vocabularyId }` ; } if ( typeof vocabulary . category !== "undefined" ) { if ( vocabulary . category . categoryName === "" ) { throw `Empty category is not allowed (check the row with vocabularyId ${ vocabulary . vocabularyId })` ; } } if ( vocabulary . definitions . length === 0 || vocabulary . definitions . filter ( ( definition ) : boolean => definition . meaning === '' ) . length > 0 ) { throw `Empty definition is not allowed (check the row with vocabularyId ${ vocabulary . vocabularyId })` ; } if ( valueByName . setId === "" ) { throw `Empty setId is not allowed (check the row with vocabularyId ${ vocabulary . vocabularyId })` ; } return [ vocabulary , valueByName . setId ] ; }