Skip to content

Instantly share code, notes, and snippets.

@shinout
Created May 17, 2016 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinout/f6e2d6780a67c10f24ef6a4ba1068097 to your computer and use it in GitHub Desktop.
Save shinout/f6e2d6780a67c10f24ef6a4ba1068097 to your computer and use it in GitHub Desktop.
更新系operationのサーバーサイドの実装
"use strict";
// @flow
//
import LifecycleManagementOperation from './lifecycle-management-operation'
/**
* 患者に処方コードを付与するOperation
*
* @extends LifecycleManagementOperation: 更新系Operation
*/
export default class PrescriptionCodeOperation extends LifecycleManagementOperation {
/**
* endpoint URL
* @protected
* これにprefixがつく
*/
static endpoint = 'prescription'
/**
* HTTP Method
* @protected
* なお、更新系Operationのdefaultは POST なので定義しなくてもいい
*/
static httpMethod = 'POST'
/**
* access制御のためにまず呼び出される
*
* @protected
* @param {string} sessionId
* @return {boolean}
*
*/
async accessControl(sessionId: string): Promise<boolean> {
const doctor = await this.domain.createRepository('doctor').getBySessionId(sessionId)
this.assign('doctor', doctor)
return doctor != null
}
/**
* 入力のvalidation
* accessControlの後に呼び出される
*
* @protected
* @param {Object} params
*
*/
async validate(params: Object) {
const {patientId, code} = params
this.validator.checkEmptyness(patientId, 'patientId', '患者idを入力してください')
this.validator.checkEmptyness(code, 'code', '処方コードを入力してください')
if (!this.errors) {
await this.validatePatient(patientId)
await this.validateCode(code)
}
}
/**
* responseの生成
*
* @protected
* @param {string} sessionId
* @return {boolean}
*
*/
async respond() {
const {doctor, patient, psCode} = this.assigned
const {hospital} = doctor
const service = this.domain.createService('patient-registration')
try {
await service.bindCodeToPatient(patient, psCode)
return true
}
catch (e) {
if (e.reason === 'scheduleAlreadySet') {
throw new Error('すでに初回受診日が設定されています')
}
}
}
/**
* responseが成功を返した場合、副作用的に実行される
* @protected
*/
async sideEffects() {
this.mailer.send('xxxx')
}
/**
* patientのvalidation
* @private
*/
async validatePatient(patientId: string): Promise {
const {doctor} = this.assigned
const patient = await this.domain.createRepository('patient').getById(patientId)
if (!patient) {
this.validationError('patientId', '存在しない患者IDです')
}
else if (patient.hospitalId !== doctor.hospitalId) {
this.validationError('patientId', '不正な患者IDです')
}
this.assign('patient', patient)
}
/**
* prescription-codeのvalidation
* @private
*/
async validateCode(code: string): Promise {
const {doctor} = this.assigned
const psCode = await this.domain.createRepository('prescription-code').getByCode(code)
if (!psCode) {
this.validationError('code', '存在しない処方コードです')
}
else if (psCode.hospitalId !== doctor.hospitalId) {
this.validationError('code', '存在しない処方コードです')
}
else if (psCode.isOccupied()) {
this.validationError('code', 'すでに利用されている処方コードです')
}
this.assign('psCode', psCode)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment