Subscription tests now pass.
This commit is contained in:
parent
1f5566ca9b
commit
4943b22a51
6 changed files with 87 additions and 75 deletions
|
@ -583,11 +583,12 @@ async function forHbs(context, listId, subscription) { // assumes grouped subscr
|
|||
};
|
||||
|
||||
if (!type.grouped && !type.enumerated) {
|
||||
entry.value = subscription ? type.forHbs(fld, subscription[fldKey]) : '';
|
||||
// subscription[fldKey] may not exists because we are getting the data from "fromPost"
|
||||
entry.value = (subscription ? type.forHbs(fld, subscription[fldKey]) : null) || '';
|
||||
|
||||
} else if (type.grouped) {
|
||||
const options = [];
|
||||
const value = subscription ? subscription[fldKey] : (type.cardinality === Cardinality.SINGLE ? null : []);
|
||||
const value = (subscription ? subscription[fldKey] : null) || (type.cardinality === Cardinality.SINGLE ? null : []);
|
||||
|
||||
for (const optCol in fld.groupedOptions) {
|
||||
const opt = fld.groupedOptions[optCol];
|
||||
|
@ -610,7 +611,7 @@ async function forHbs(context, listId, subscription) { // assumes grouped subscr
|
|||
|
||||
} else if (type.enumerated) {
|
||||
const options = [];
|
||||
const value = subscription ? subscription[fldKey] : null;
|
||||
const value = (subscription ? subscription[fldKey] : null) || null;
|
||||
|
||||
for (const opt of fld.settings.options) {
|
||||
options.push({
|
||||
|
@ -631,7 +632,8 @@ async function forHbs(context, listId, subscription) { // assumes grouped subscr
|
|||
}
|
||||
|
||||
// Converts subscription data received via POST request from subscription form or via subscribe request to API v1 to subscription structure supported by subscriptions model.
|
||||
async function fromPost(context, listId, data) { // assumes grouped subscription
|
||||
// If a field is not specified in the POST data, it omits it also in the returned subscription
|
||||
async function fromPost(context, listId, data, partial) { // assumes grouped subscription
|
||||
|
||||
// This is to handle option values from API v1
|
||||
function isSelected(value) {
|
||||
|
@ -643,43 +645,45 @@ async function fromPost(context, listId, data) { // assumes grouped subscription
|
|||
const subscription = {};
|
||||
|
||||
for (const fld of flds) {
|
||||
const type = fieldTypes[fld.type];
|
||||
const fldKey = getFieldKey(fld);
|
||||
if (fld.key in data) {
|
||||
const type = fieldTypes[fld.type];
|
||||
const fldKey = getFieldKey(fld);
|
||||
|
||||
let value = null;
|
||||
let value = null;
|
||||
|
||||
if (!type.grouped && !type.enumerated) {
|
||||
value = type.parsePostValue(fld, cleanupFromPost(data[fld.key]));
|
||||
if (!type.grouped && !type.enumerated) {
|
||||
value = type.parsePostValue(fld, cleanupFromPost(data[fld.key]));
|
||||
|
||||
} else if (type.grouped) {
|
||||
if (type.cardinality === Cardinality.SINGLE) {
|
||||
for (const optCol in fld.groupedOptions) {
|
||||
const opt = fld.groupedOptions[optCol];
|
||||
} else if (type.grouped) {
|
||||
if (type.cardinality === Cardinality.SINGLE) {
|
||||
for (const optCol in fld.groupedOptions) {
|
||||
const opt = fld.groupedOptions[optCol];
|
||||
|
||||
// This handles two different formats for grouped dropdowns and radios.
|
||||
// The first part of the condition handles the POST requests from the subscription form, while the
|
||||
// second part handles the subscribe request to API v1
|
||||
if (data[fld.key] === opt.key || isSelected(data[opt.key])) {
|
||||
value = opt.column
|
||||
// This handles two different formats for grouped dropdowns and radios.
|
||||
// The first part of the condition handles the POST requests from the subscription form, while the
|
||||
// second part handles the subscribe request to API v1
|
||||
if (data[fld.key] === opt.key || isSelected(data[opt.key])) {
|
||||
value = opt.column
|
||||
}
|
||||
}
|
||||
} else {
|
||||
value = [];
|
||||
|
||||
for (const optCol in fld.groupedOptions) {
|
||||
const opt = fld.groupedOptions[optCol];
|
||||
|
||||
if (isSelected(data[opt.key])) {
|
||||
value.push(opt.column);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
value = [];
|
||||
|
||||
for (const optCol in fld.groupedOptions) {
|
||||
const opt = fld.groupedOptions[optCol];
|
||||
|
||||
if (isSelected(data[opt.key])) {
|
||||
value.push(opt.column);
|
||||
}
|
||||
}
|
||||
} else if (type.enumerated) {
|
||||
value = data[fld.key];
|
||||
}
|
||||
|
||||
} else if (type.enumerated) {
|
||||
value = data[fld.key];
|
||||
subscription[fldKey] = value;
|
||||
}
|
||||
|
||||
subscription[fldKey] = value;
|
||||
}
|
||||
|
||||
return subscription;
|
||||
|
|
|
@ -533,6 +533,7 @@ async function enforceEntityPermission(context, entityTypeId, entityId, required
|
|||
await knex.transaction(async tx => {
|
||||
const result = await _checkPermissionTx(tx, context, entityTypeId, entityId, requiredOperations);
|
||||
if (!result) {
|
||||
log.info(`Denying permission ${entityTypeId}.${entityId} ${requiredOperations}`);
|
||||
throwPermissionDenied();
|
||||
}
|
||||
});
|
||||
|
@ -544,6 +545,7 @@ async function enforceEntityPermissionTx(tx, context, entityTypeId, entityId, re
|
|||
}
|
||||
const result = await _checkPermissionTx(tx, context, entityTypeId, entityId, requiredOperations);
|
||||
if (!result) {
|
||||
log.info(`Denying permission ${entityTypeId}.${entityId} ${requiredOperations}`);
|
||||
throwPermissionDenied();
|
||||
}
|
||||
}
|
||||
|
@ -552,6 +554,7 @@ async function enforceTypePermission(context, entityTypeId, requiredOperations)
|
|||
await knex.transaction(async tx => {
|
||||
const result = await _checkPermissionTx(tx, context, entityTypeId, null, requiredOperations);
|
||||
if (!result) {
|
||||
log.info(`Denying permission ${entityTypeId} ${requiredOperations}`);
|
||||
throwPermissionDenied();
|
||||
}
|
||||
});
|
||||
|
@ -560,6 +563,7 @@ async function enforceTypePermission(context, entityTypeId, requiredOperations)
|
|||
async function enforceTypePermissionTx(tx, context, entityTypeId, requiredOperations) {
|
||||
const result = await _checkPermissionTx(tx, context, entityTypeId, null, requiredOperations);
|
||||
if (!result) {
|
||||
log.info(`Denying permission ${entityTypeId} ${requiredOperations}`);
|
||||
throwPermissionDenied();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -400,12 +400,18 @@ async function _validateAndPreprocess(tx, listId, groupedFieldsMap, entity, meta
|
|||
}
|
||||
const existingWithKey = await existingWithKeyQuery.first();
|
||||
if (existingWithKey) {
|
||||
if (meta && (meta.updateAllowed || meta.updateOfUnsubscribedAllowed && existingWithKey.status === SubscriptionStatus.UNSUBSCRIBED)) {
|
||||
if (meta && (meta.updateAllowed || (meta.updateOfUnsubscribedAllowed && existingWithKey.status === SubscriptionStatus.UNSUBSCRIBED))) {
|
||||
meta.update = true;
|
||||
meta.existing = existingWithKey;
|
||||
} else {
|
||||
throw new interoperableErrors.DuplicitEmailError();
|
||||
}
|
||||
} else {
|
||||
// This is here because of the API, which allows one to send subscriptions without caring about whether they already exist, what their status is, etc.
|
||||
// In the case, the subscription is existing, we should not change the status. If it does not exist, we are fine with changing the status
|
||||
if (meta.subscribeIfNoExisting && !entity.status) {
|
||||
entity.status = SubscriptionStatus.SUBSCRIBED;
|
||||
}
|
||||
}
|
||||
|
||||
if ((isCreate && !(meta && meta.update)) || 'status' in entity) {
|
||||
|
@ -455,8 +461,8 @@ async function _create(tx, listId, filteredEntity) {
|
|||
|
||||
/*
|
||||
Adds a new subscription. Returns error if a subscription with the same email address is already present and is not unsubscribed.
|
||||
If it is unsubscribed, the existing subscription is changed based on the provided data.
|
||||
If meta.partial is true, it updates even an active subscription.
|
||||
If it is unsubscribed and meta.updateOfUnsubscribedAllowed, the existing subscription is changed based on the provided data.
|
||||
If meta.updateAllowed is true, it updates even an active subscription.
|
||||
*/
|
||||
async function create(context, listId, entity, meta /* meta is provided when called from /confirm/subscribe/:cid */) {
|
||||
return await knex.transaction(async tx => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue