import { DeepPartial , Omit } from '@ulangi/extended-types' ; import { DefinitionStatus } from '@ulangi/ulangi-common/enums' ; import { Definition } from '@ulangi/ulangi-common/interfaces' ; import { AbstractPreparer } from '@ulangi/ulangi-common/preparers' ; import * as Joi from 'joi' ; import * as _ from 'lodash' ; import { DefinitionRow , DefinitionRowForInsert , DefinitionRowForUpdate , } from '../interfaces/DefinitionRow' ; export class DefinitionRowPreparer extends AbstractPreparer < DefinitionRow > { protected insertRules = { userId : Joi . string ( ) , definitionId : Joi . string ( ) , vocabularyId : Joi . string ( ) , definitionStatus : Joi . string ( ) . valid ( _ . values ( DefinitionStatus ) ) , meaning : Joi . string ( ) , wordClasses : Joi . string ( ) , source : Joi . string ( ) , createdAt : Joi . date ( ) , updatedAt : Joi . date ( ) , updatedStatusAt : Joi . date ( ) , firstSyncedAt : Joi . forbidden ( ) . strip ( ) . optional ( ) , lastSyncedAt : Joi . forbidden ( ) . strip ( ) . optional ( ) , } ; protected updateRules = { userId : Joi . string ( ) , definitionId : Joi . string ( ) , vocabularyId : Joi . string ( ) . optional ( ) , definitionStatus : Joi . string ( ) . valid ( _ . values ( DefinitionStatus ) ) . optional ( ) , meaning : Joi . string ( ) . optional ( ) , wordClasses : Joi . string ( ) . optional ( ) , source : Joi . string ( ) . optional ( ) , createdAt : Joi . date ( ) . optional ( ) , updatedAt : Joi . date ( ) . optional ( ) , updatedStatusAt : Joi . date ( ) . optional ( ) , firstSyncedAt : Joi . forbidden ( ) . strip ( ) . optional ( ) , lastSyncedAt : Joi . forbidden ( ) . strip ( ) . optional ( ) , } ; public prepareInsert ( userId , definition , vocabularyId ) ; public prepareInsert ( userId , definition , vocabularyId ) { const definitionRow = this . convertToInsertRow ( userId , definition , vocabularyId ) ; return this . validateData ( definitionRow , this . insertRules ) as DefinitionRowForInsert ; } public prepareUpdate ( userId , definition , vocabularyId ) { const definitionRow = this . convertToUpdateRow ( userId , definition , vocabularyId ) ; return _ . omitBy ( this . validateData ( definitionRow , this . updateRules ) , _ . isUndefined ) ; } public canPrepareInsert ( userId , definition , vocabularyId ) ; public canPrepareInsert ( userId , definition , vocabularyId ) { const definitionRow = this . convertToInsertRow ( userId , definition , vocabularyId ) ; return this . isValidData ( definitionRow , this . insertRules ) ; } public canPrepareUpdate ( userId , definition , vocabularyId ) { const definitionRow = this . convertToUpdateRow ( userId , definition , vocabularyId ) ; return this . isValidData ( definitionRow , this . updateRules ) ; } private convertToInsertRow ( userId , definition , vocabularyId ) { const definitionRow = { userId , definitionId : definition . definitionId , vocabularyId , definitionStatus : definition . definitionStatus , meaning : definition . meaning , wordClasses : JSON . stringify ( definition . wordClasses ) , source : definition . source , createdAt : definition . createdAt , updatedAt : definition . updatedAt , updatedStatusAt : definition . updatedStatusAt , } ; return definitionRow ; } private convertToUpdateRow ( userId , definition , vocabularyId ) { const definitionRow = { userId , definitionId : definition . definitionId , vocabularyId , definitionStatus : definition . definitionStatus , meaning : definition . meaning , wordClasses : typeof definition . wordClasses !== 'undefined' ? JSON . stringify ( definition . wordClasses ) : definition . wordClasses , source : definition . source , createdAt : definition . createdAt , updatedAt : definition . updatedAt , updatedStatusAt : definition . updatedStatusAt , firstSyncedAt : undefined , lastSyncedAt : undefined , } ; return definitionRow ; } }