1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- import _ from "lodash";
- import AccountSerializer from "./account.serializer.js";
- import AccountService from "./account.service.js";
- import AccountValidator from "./account.validator.js";
- class Controller {
- async byId(req, res) {
- const account = await AccountService.byId(req.params.id);
- if (account) res.json(AccountSerializer.show(account));
- else res.status(404).end();
- }
- async update(req, res) {
- const accountData = _.pick(req.body, [
- "companyName",
- "companyVat",
- "companyBillingAddress",
- "companySdi",
- "companyPhone",
- "companyEmail",
- "companyPec",
- "companyCountry",
- "planType", // Added planType to allowed fields
- ]);
- const accountErrors = await AccountValidator.onUpdate(accountData);
- if (accountErrors) {
- return res.status(422).json({
- success: false,
- errors: accountErrors.details,
- });
- }
- const result = await AccountService.update(req.params.id, accountData);
- if (result) {
- return res.json(AccountSerializer.show(result));
- } else {
- return res.status(422).json({
- success: false,
- message: "Failed to update account.",
- });
- }
- }
- // New controller method to set trial period
- async setTrialPeriod(req, res) {
- try {
- const { id: accountId } = req.params;
- const { trialDays } = req.body;
-
- console.log(`Received request to set trial period for account ${accountId} to ${trialDays} days`);
-
- // Validate input
- if (!trialDays || isNaN(parseInt(trialDays)) || parseInt(trialDays) < 0) {
- console.log('Validation failed: Invalid trial days value');
- return res.status(422).json({
- success: false,
- message: "Trial days must be a valid positive number"
- });
- }
-
- const trialDaysInt = parseInt(trialDays);
- console.log(`Parsed trial days: ${trialDaysInt}`);
-
- // Call service method to update the trial period
- const result = await AccountService.setTrialPeriod(accountId, trialDaysInt);
-
- if (result) {
- console.log('Trial period set successfully');
- return res.json(AccountSerializer.show(result));
- } else {
- console.log('Failed to update trial period');
- return res.status(422).json({
- success: false,
- message: "Failed to update trial period."
- });
- }
- } catch (error) {
- console.error('Error in setTrialPeriod controller:', error);
- return res.status(500).json({
- success: false,
- message: "Server error while updating trial period",
- error: error.message
- });
- }
- }
- }
- export default new Controller();
|