2018-04-04 22:10:34 +00:00
|
|
|
const is = require('@sindresorhus/is');
|
|
|
|
|
|
|
|
const Errors = require('./types/errors');
|
|
|
|
const Settings = require('./settings');
|
|
|
|
|
|
|
|
|
|
|
|
exports.syncReadReceiptConfiguration = async ({
|
|
|
|
deviceId,
|
|
|
|
sendRequestConfigurationSyncMessage,
|
|
|
|
storage,
|
|
|
|
}) => {
|
|
|
|
if (!is.string(deviceId)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'deviceId' is required");
|
2018-04-04 22:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!is.function(sendRequestConfigurationSyncMessage)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'sendRequestConfigurationSyncMessage' is required");
|
2018-04-04 22:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!is.object(storage)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'storage' is required");
|
2018-04-04 22:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const isPrimaryDevice = deviceId === '1';
|
|
|
|
if (isPrimaryDevice) {
|
|
|
|
return {
|
|
|
|
status: 'skipped',
|
|
|
|
reason: 'isPrimaryDevice',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const settingName = Settings.READ_RECEIPT_CONFIGURATION_SYNC;
|
|
|
|
const hasPreviouslySynced = Boolean(storage.get(settingName));
|
|
|
|
if (hasPreviouslySynced) {
|
|
|
|
return {
|
|
|
|
status: 'skipped',
|
|
|
|
reason: 'hasPreviouslySynced',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await sendRequestConfigurationSyncMessage();
|
|
|
|
storage.put(settingName, true);
|
|
|
|
} catch (error) {
|
|
|
|
return {
|
|
|
|
status: 'error',
|
|
|
|
reason: 'failedToSendSyncMessage',
|
|
|
|
error: Errors.toLogFormat(error),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
status: 'complete',
|
|
|
|
};
|
|
|
|
};
|