const jingrow_cloud_base_endpoint = 'https://jcloud.jingrow.com';
function calculate_trial_end_days() {
// try to check for trial_end_date in jingrow.boot.subscription_conf
if (jingrow.boot.subscription_conf.trial_end_date) {
const trial_end_date = new Date(
jingrow.boot.subscription_conf.trial_end_date,
);
const today = new Date();
const diffTime = trial_end_date - today;
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
return diffDays;
} else {
return 15 - jingrow.boot.telemetry_site_age;
}
}
const trial_end_days = calculate_trial_end_days();
const trial_end_string =
trial_end_days > 1 ? `${trial_end_days} days` : `${trial_end_days} day`;
let subscription_string = __(
`Your trial ends in ${trial_end_string}. Please subscribe for uninterrupted services`,
);
let $floatingBar = $(`
${subscription_string}
×
`);
$(document).ready(function () {
if (jingrow.boot.setup_complete === 1) {
if (
!jingrow.is_mobile() &&
jingrow.boot.subscription_conf.status !== 'Subscribed' &&
trial_end_days > 0
) {
$('.layout-main-section').before($floatingBar);
$floatingBar.find('.dismiss-upgrade').on('click', () => {
$floatingBar.remove();
});
}
if (jingrow.user.has_role('System Manager')) {
add_jingrow_cloud_dashboard_link();
}
}
});
function add_jingrow_cloud_dashboard_link() {
$('.dropdown-navbar-user .dropdown-menu .dropdown-divider').before(
`Log In to Jingrow Cloud`,
);
}
function showBanner() {
const d = new jingrow.ui.Dialog({
title: __('Change Plan'),
size: 'medium',
});
$(d.body).html(`
`);
d.show();
}
// Jingrow Cloud login related
function initiateRequestForLoginToFrappeCloud() {
jingrow.confirm(
'Are you sure you want to login to Jingrow Cloud dashboard ?',
() => {
requestLoginToFC();
},
);
}
function requestLoginToFC(freezing_msg) {
jingrow.request.call({
url: `${jingrow_cloud_base_endpoint}/api/method/jcloude.api.developer.saas.send_verification_code`,
type: 'POST',
args: {
domain: window.location.hostname,
},
freeze: true,
freeze_message: freezing_msg || 'Initiating login to Jingrow Cloud',
success: function (r) {
showFCLogindialog(r.message.email);
setErrorMessage('');
},
error: function (r) {
jingrow.throw('Failed to login to Jingrow Cloud. Please try again');
},
});
}
function setErrorMessage(message) {
$('#fc-login-error').text(message);
}
function showFCLogindialog(email) {
if (!window.fc_login_dialog) {
var d = new jingrow.ui.Dialog({
title: __('Login to Jingrow Cloud'),
primary_action_label: __('Verify', null, 'Submit verification code'),
primary_action: verifyCode,
});
$(d.body).html(
repl(
`
We have sent the verification code to your email id ${email}
`,
jingrow.app,
),
);
d.add_custom_action("Didn't receive code? Resend", () => {
d.hide();
requestLoginToFC('Resending Verification Code...');
});
window.fc_login_dialog = d;
}
function verifyCode() {
let otp = $('#fc-login-verification-code').val();
if (!otp) {
return;
}
jingrow.request.call({
url: `${jingrow_cloud_base_endpoint}/api/method/jcloude.api.developer.saas.verify_verification_code`,
type: 'POST',
args: {
domain: window.location.hostname,
verification_code: otp,
},
freeze: true,
silent: true,
freeze_message: 'Validating verification code',
success: function (r) {
if (r.login_token) {
fc_login_dialog.hide();
window.open(
`${jingrow_cloud_base_endpoint}/api/method/jcloude.api.developer.saas.login_to_fc?token=${r.login_token}`,
'_blank',
);
jingrow.msgprint({
title: __('Jingrow Cloud Login Successful'),
indicator: 'green',
message: __(
`You will be redirected to Jingrow Cloud soon.
If you haven\'t been redirected, Click here to login
`,
),
});
} else {
setErrorMessage('Login failed. Please try again');
}
},
error: function (r) {
if (r.exc) {
setErrorMessage(
JSON.parse(JSON.parse(r._server_messages)[0])['message'],
);
}
},
});
}
fc_login_dialog.show();
}