oak-pay-business/es/checkers/withdrawAccount.js

72 lines
2.4 KiB
JavaScript

import { generateNewId } from 'oak-domain/lib/utils/uuid';
import { pipeline } from 'oak-domain/lib/utils/executor';
import assert from 'assert';
const checkers = [
{
entity: 'withdrawAccount',
type: 'logical',
action: 'create',
checker(operation, context, option) {
const { data } = operation;
if (data) {
const { id, entity, entityId, isDefault } = data;
if (entity && entityId && isDefault) {
return context.operate('withdrawAccount', {
id: generateNewId(),
action: 'update',
data: {
isDefault: false,
},
filter: {
entity,
entityId,
isDefault: true,
id: {
$ne: id,
},
}
}, option);
}
}
}
},
{
entity: 'withdrawAccount',
type: 'logical',
action: 'update',
checker(operation, context, option) {
const { data, filter } = operation;
if (data?.isDefault) {
return pipeline(() => context.select('withdrawAccount', {
data: {
id: 1,
entity: 1,
entityId: 1,
},
filter,
}, {}), (accounts) => {
assert(accounts.length === 1);
const [account] = accounts;
const { entity, entityId, id } = account;
return context.operate('withdrawAccount', {
id: generateNewId(),
action: 'update',
data: {
isDefault: false,
},
filter: {
entity,
entityId,
isDefault: true,
id: {
$ne: id,
},
}
}, option);
});
}
}
}
];
export default checkers;