131 lines
4.6 KiB
JavaScript
131 lines
4.6 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const uuid_1 = require("oak-domain/lib/utils/uuid");
|
|
const executor_1 = require("oak-domain/lib/utils/executor");
|
|
const assert_1 = require("oak-domain/lib/utils/assert");
|
|
const checkers = [
|
|
{
|
|
entity: 'applicationPassport',
|
|
type: 'logical',
|
|
action: 'create',
|
|
checker(operation, context, option) {
|
|
const { data } = operation;
|
|
if (data) {
|
|
const { id, applicationId, isDefault } = data;
|
|
if (applicationId && isDefault) {
|
|
return context.operate('applicationPassport', {
|
|
id: (0, uuid_1.generateNewId)(),
|
|
action: 'update',
|
|
data: {
|
|
isDefault: false,
|
|
},
|
|
filter: {
|
|
applicationId,
|
|
isDefault: true,
|
|
id: {
|
|
$ne: id,
|
|
},
|
|
}
|
|
}, option);
|
|
}
|
|
}
|
|
}
|
|
},
|
|
{
|
|
entity: 'applicationPassport',
|
|
type: 'logical',
|
|
action: 'update',
|
|
checker(operation, context, option) {
|
|
const { data, filter } = operation;
|
|
if (data?.isDefault) {
|
|
return (0, executor_1.pipeline)(() => context.select('applicationPassport', {
|
|
data: {
|
|
id: 1,
|
|
applicationId: 1,
|
|
},
|
|
filter,
|
|
}, {}), (applicationPassports) => {
|
|
(0, assert_1.assert)(applicationPassports.length === 1);
|
|
const [applicationPassport] = applicationPassports;
|
|
const { applicationId, id } = applicationPassport;
|
|
return context.operate('applicationPassport', {
|
|
id: (0, uuid_1.generateNewId)(),
|
|
action: 'update',
|
|
data: {
|
|
isDefault: false,
|
|
},
|
|
filter: {
|
|
applicationId,
|
|
isDefault: true,
|
|
id: {
|
|
$ne: id,
|
|
},
|
|
}
|
|
}, option);
|
|
});
|
|
}
|
|
}
|
|
},
|
|
{
|
|
entity: 'applicationPassport',
|
|
type: 'logical',
|
|
action: 'remove',
|
|
checker(operation, context, option) {
|
|
const { filter } = operation;
|
|
(0, assert_1.assert)(filter);
|
|
const remove = context.select('applicationPassport', {
|
|
data: {
|
|
id: 1,
|
|
applicationId: 1,
|
|
isDefault: 1,
|
|
},
|
|
filter,
|
|
}, { forUpdate: true });
|
|
const updateDefaultFn = (id, applicationId) => {
|
|
return (0, executor_1.pipeline)(() => context.select('applicationPassport', {
|
|
data: {
|
|
id: 1,
|
|
applicationId: 1,
|
|
isDefault: 1,
|
|
},
|
|
filter: {
|
|
id: {
|
|
$ne: id,
|
|
},
|
|
isDefault: false,
|
|
applicationId,
|
|
},
|
|
indexFrom: 0,
|
|
count: 1,
|
|
}, {}), (other) => {
|
|
if (other && other.length === 1) {
|
|
return context.operate('applicationPassport', {
|
|
id: (0, uuid_1.generateNewId)(),
|
|
action: 'update',
|
|
data: {
|
|
isDefault: true,
|
|
},
|
|
filter: {
|
|
id: other[0].id,
|
|
},
|
|
}, option);
|
|
}
|
|
});
|
|
};
|
|
if (remove instanceof Promise) {
|
|
return remove.then((r) => {
|
|
if (r[0]?.isDefault) {
|
|
return updateDefaultFn(r[0].id, r[0].applicationId);
|
|
}
|
|
});
|
|
}
|
|
else {
|
|
if (remove[0]?.isDefault) {
|
|
return updateDefaultFn(remove[0].id, remove[0].applicationId);
|
|
}
|
|
}
|
|
}
|
|
},
|
|
];
|
|
exports.default = checkers;
|