import { DeepPartial } from '@ulangi/extended-types' ; import { VocabularyStatus } from '@ulangi/ulangi-common/enums' ; import { Vocabulary } from '@ulangi/ulangi-common/interfaces' ; import { AbstractPreparer } from '@ulangi/ulangi-common/preparers' ; import * as Joi from 'joi' ; import * as _ from 'lodash' ; import { VocabularyRow , VocabularyRowForInsert , VocabularyRowForUpdate , } from '../interfaces/VocabularyRow' ; export class VocabularyRowPreparer extends AbstractPreparer < VocabularyRow > { protected insertRules = { userId : Joi . string ( ) , vocabularyId : Joi . string ( ) , setId : Joi . string ( ) , vocabularyStatus : Joi . string ( ) . valid ( _ . values ( VocabularyStatus ) ) , vocabularyText : Joi . string ( ) , level : Joi . number ( ) , lastLearnedAt : Joi . date ( ) . allow ( null ) , createdAt : Joi . date ( ) , updatedAt : Joi . date ( ) , updatedStatusAt : Joi . date ( ) , firstSyncedAt : Joi . forbidden ( ) . strip ( ) . optional ( ) , lastSyncedAt : Joi . forbidden ( ) . strip ( ) . optional ( ) , } ; protected updateRules = { userId : Joi . string ( ) , vocabularyId : Joi . string ( ) , setId : Joi . string ( ) . optional ( ) , vocabularyStatus : Joi . string ( ) . valid ( _ . values ( VocabularyStatus ) ) . optional ( ) , vocabularyText : Joi . string ( ) . optional ( ) , level : Joi . number ( ) . optional ( ) , createdAt : Joi . date ( ) . optional ( ) , lastLearnedAt : Joi . date ( ) . allow ( null ) . optional ( ) , updatedAt : Joi . date ( ) . optional ( ) , updatedStatusAt : Joi . date ( ) . optional ( ) , firstSyncedAt : Joi . forbidden ( ) . strip ( ) . optional ( ) , lastSyncedAt : Joi . forbidden ( ) . strip ( ) . optional ( ) , } ; public prepareInsert ( userId , vocabulary , setId ) ; public prepareInsert ( userId , vocabulary , setId ) { const vocabularyRow = this . convertToInsertRow ( userId , vocabulary , setId ) ; return this . validateData ( vocabularyRow , this . insertRules ) as VocabularyRowForInsert ; } public prepareUpdate ( userId , vocabulary , setId ) { const vocabularyRow = this . convertToUpdateRow ( userId , vocabulary , setId ) ; return _ . omitBy ( this . validateData ( vocabularyRow , this . updateRules ) , _ . isUndefined ) ; } public canPrepareInsert ( userId , vocabulary , setId ) ; public canPrepareInsert ( userId , vocabulary , setId ) { const vocabularyRow = this . convertToInsertRow ( userId , vocabulary , setId ) ; return this . isValidData ( vocabularyRow , this . insertRules ) ; } public canPrepareUpdate ( userId , vocabulary , setId ) { const vocabularyRow = this . convertToUpdateRow ( userId , vocabulary , setId ) ; return this . isValidData ( vocabularyRow , this . updateRules ) ; } private convertToInsertRow ( userId , vocabulary , setId ) { const vocabularyRow = { userId , vocabularyId : vocabulary . vocabularyId , setId , vocabularyStatus : vocabulary . vocabularyStatus , vocabularyText : vocabulary . vocabularyText , level : vocabulary . level , lastLearnedAt : vocabulary . lastLearnedAt , createdAt : vocabulary . createdAt , updatedAt : vocabulary . updatedAt , updatedStatusAt : vocabulary . updatedStatusAt , } ; return vocabularyRow ; } private convertToUpdateRow ( userId , vocabulary , setId ) { const vocabularyRow = { userId , vocabularyId : vocabulary . vocabularyId , setId , vocabularyStatus : vocabulary . vocabularyStatus , vocabularyText : vocabulary . vocabularyText , level : vocabulary . level , lastLearnedAt : vocabulary . lastLearnedAt , createdAt : vocabulary . createdAt , updatedAt : vocabulary . updatedAt , updatedStatusAt : vocabulary . updatedStatusAt , firstSyncedAt : undefined , lastSyncedAt : undefined , } ; return vocabularyRow ; } }