UNPKG

@andreabiagini5/applicazioni-e-servizi-web-project

Version:
123 lines 4.82 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.deleteAccount = exports.updateRating = exports.updateEmail = exports.getAccount = exports.authenticateAccount = exports.registerAccount = void 0; const repository = __importStar(require("../repositories/account")); /** * Register a new account * @param {Account} account - The account to register * @returns boolean indicating success or failure */ const registerAccount = async (account) => { const isExistingAccount = await (0, exports.getAccount)(account.username); if (isExistingAccount) return false; await repository.createAccount(account); return true; }; exports.registerAccount = registerAccount; /** * Authenticate an already existing user. * @param username the username of the account * @param password the password that should match the stored password * @returns {Account | null} the authenticated account or null if not found */ const authenticateAccount = async (username, password) => { const account = await (0, exports.getAccount)(username); if (!account || !(await account.checkPassword(password))) return null; return account; }; exports.authenticateAccount = authenticateAccount; /** * Retrieves the account associated with the given username. * @param username the username of the account * @returns {Account | null} the account associated with the username or null if not found */ const getAccount = async (username) => { const existingAccounts = await repository.readAllAccounts(); const account = existingAccounts.find(a => a.username === username); if (!account) return null; return account; }; exports.getAccount = getAccount; /** * Updates the email of the given account. * @param account the account to update * @param newEmail the new email to set * @returns {boolean} true if the email was updated successfully, false otherwise * @throws {Error} if the account is not found */ const updateEmail = async (account, newEmail) => { const existingAccounts = await repository.readAllAccounts(); const isExistingAccount = existingAccounts.find(a => a.email === newEmail); if (isExistingAccount) return false; const validEmail = account.changeEmail(newEmail); if (!validEmail) return false; await repository.updateAccount(account); return true; }; exports.updateEmail = updateEmail; /** * Updates the rating of the given account. * @param account the account to update * @param newRating the new rating to set * @returns true if the rating was updated successfully, false otherwise */ const updateRating = async (account, newRating) => { const success = account.changeRating(newRating); if (success) { await repository.updateAccount(account); } return success; }; exports.updateRating = updateRating; /** * Deletes the account associated with the given account object. * @param username the username of the account to delete * @returns {Promise<boolean>} true if the account was deleted successfully, false otherwise * @throws {Error} if the account is not found */ const deleteAccount = async (username) => { const existingAccount = await (0, exports.getAccount)(username); if (!existingAccount) throw new Error('Account not found'); return repository.deleteAccount(existingAccount); }; exports.deleteAccount = deleteAccount; //# sourceMappingURL=account.js.map