Best JavaScript code snippet using qawolf
auth.service.ts
Source:auth.service.ts
1import { Injectable } from '@nestjs/common';2import { Model } from 'mongoose';3import { InjectModel } from '@nestjs/mongoose';4import { User } from '../users/user.schema';5import { Response } from 'express';6import { LoginDTO, RegisterDTO } from './dtos/auth.dto';7import { verify, hash } from 'argon2';8import { generateKey } from '../utils/environment';9import { getUser, addGuildMember } from '../utils/environment/oauth';10@Injectable()11export class AuthService {12 constructor (13 @InjectModel(User.name) private users: Model<User>14 ) {}15 /**16 * Login a user.17 * @param res HTTP Response.18 * @param body Request body, user's credentials.19 * @param session User's session, if exists.20 */21 async login(res: Response, body: LoginDTO, session: any) {22 if (session.user) {23 return res.json({ success: false, message: 'You are already logged in.' });24 } else {25 const { username, password } = body;26 if (username && password) {27 const user = await this.users.findOne({ 'user.username': username });28 if (user) {29 verify(user.user.password, password)30 .then((validPassword) => {31 if (validPassword) {32 delete user.user.password;33 session.user = user;34 return res.json({ success: true, message: 'Logged in successfully.' });35 } else {36 return res.json({ success: false, message: 'Invalid password.' });37 }38 }).catch(() => {39 return res.json({ success: false, message: 'Something went wrong, please DM aspect#6910.' });40 });41 } else {42 return res.json({ success: false, message: 'Invalid username.' });43 }44 } else {45 return res.json({ success: false, message: 'Invalid fields.' });46 }47 }48 }49 /**50 * Register a user.51 * @param res HTTP Response52 * @param body Request body, user's credentials.53 * @param session User's session, if exists.54 */55 async register(res: Response, body: RegisterDTO, session: any) {56 if (session.user) {57 return res.json({ success: false, message: 'You are already logged in.' });58 } else {59 const { username, password, invite } = body;60 if (username && password && invite) {61 if (username.trim().length < 1 || password.trim().length < 3) {62 return res.json({ success: false, message: 'Your username/password is too short.' });63 } else {64 if (await this.users.findOne({ 'user.username': { $regex: new RegExp(username, 'i') } })) {65 return res.json({ success: false, message: 'This username already exists.' });66 } else {67 const findInvite = await this.users.findOne({ 'user.invite': invite });68 if (findInvite) {69 if (findInvite.user.username !== null) {70 return res.json({ success: false, message: 'This invite has already been redeemed.' });71 } else {72 if (findInvite.stats.invitedBy !== 'Unknown') {73 const inviter = await this.users.findOne({ 'user.username': findInvite.stats.invitedBy });74 if (inviter) {75 await this.users.updateOne({ 'user.invite': inviter.user.invite }, {76 $push: {77 'stats.invitedUsers': username,78 },79 });80 }81 }82 await this.users.updateOne(findInvite, {83 'user.username': username,84 'user.password': await hash(password),85 'user.key': generateKey(),86 'stats.registrationDate': new Date().toLocaleDateString(),87 }).then(async () => {88 session.user = await this.users.findOne({ 'user.invite': invite });89 return res.json({ success: true, message: 'Registered successfully.' });90 }).catch((err) => {91 return res.json({ success: false, message: err.message });92 });93 }94 } else {95 return res.json({ success: false, message: 'Invalid invite code.' });96 }97 }98 }99 } else {100 return res.json({ success: false, message: 'Invalid fields.' });101 }102 }103 }104 /**105 * Redirect a user to the auth uri.106 * @param res HTTP Response.e107 * @param session User's session.108 */109 redirect(res: Response, session: any) {110 if (session.user) {111 res.redirect(process.env.DISCORD_URI);112 } else {113 res.redirect('http://localhost:3000');114 }115 }116 /**117 * Link a user's discord account to their account.118 * @param res HTTP Response.119 * @param session User's session.120 * @param code OAuth2 Code.121 */122 async linkAccount(res: Response, session: any, code: string) {123 if (session.user) {124 if (code) {125 const data = (await getUser(code));126 if (data.user) {127 await addGuildMember(data)128 .then(async () => {129 await this.users.updateOne({130 'user.username': session.user.user.username,131 }, {132 'user.discordId': data.user.id,133 'user.discordAvatar': data.user.avatar,134 });135 });136 session.user = await this.users.findOne({ 'user.discordId': data.user.id });137 res.redirect('http://localhost:3001/dashboard');138 } else {139 res.redirect(process.env.DISCORD_URI);140 }141 } else {142 res.redirect(process.env.DISCORD_URI);143 }144 } else {145 res.redirect('http://localhost:3001');146 }147 }148 149 logout(res: Response, session: any) {150 if (session && session.user) {151 session.destroy();152 return res.json({ success: true, message: 'Logged out successfully.' });153 } else {154 return res.json({ success: false, message: 'Invalid session.' });155 }156 }...
manager.service.js
Source:manager.service.js
1// SERVICES2foundITApp.service('managerService', function ($q, httpService) {3 var self = {4 getJobList: function () {5 var d = $q.defer();6 httpService.getData('api/jobs').then(function success(response) {7 console.log('managerService getData response', response);8 d.resolve(response.data);9 }, function error(err) {10 console.log('managerService::getData Error', err);11 d.reject(err);12 });13 return d.promise;14 },15 getReviewers: function () {16 var d = $q.defer();17 httpService.getData('api/admin/reviewers').then(function success(response) {18 console.log('managerService getReviewers response', response);19 d.resolve(response.data);20 }, function error(err) {21 console.log('managerService::getReviewers Error', err);22 d.reject(err);23 });24 return d.promise;25 },26 createJob: function (job) {27 var d = $q.defer();28 httpService.postData('api/jobs', job).then(function success(response) {29 console.log('managerService createJob response', response);30 d.resolve(response.data);31 }, function error(err) {32 console.log('managerService::createJob error', err);33 d.reject(err);34 });35 return d.promise;36 },37 upDateJob: function (job) {38 var d = $q.defer();39 httpService.putData('api/jobs', job).then(function success(response) {40 console.log('managerService createJob response', response);41 d.resolve(response.data);42 }, function error(err) {43 console.log('managerService::upDateJob error', err);44 d.reject(err);45 });46 return d.promise;47 },48 deleteJob: function (jobId) {49 var d = $q.defer();50 httpService.deleteData('api/jobs/' + jobId).then(function success() {51 d.resolve();52 }, function error(err) {53 console.log('managerService::deleteJob error', err);54 d.reject(err);55 });56 return d.promise;57 },58 getJobApplyCombo: function () {59 var d = $q.defer();60 // api/apply/combo todo: replace back to api61 httpService.getData('api/apply/combo').then(function success(response) {62 console.log('managerService getJobApplyCombo response data', response.data);63 console.log(response.data);64 var approvedApps = [];65 response.data.forEach(function (item) {66 //console.log(item.applicationStatus);67 if (item.jobStatus === 'REVIEW_PROCESSING' &&68 item.applicationStatus === 'APPROVED') {69 approvedApps.push(item);70 }71 });72 console.log('approved apps', approvedApps);73 // self.__hello();74 var finalInviteList = [];75 approvedApps.forEach(function (approvedJob) {76 var findInvite = self.__findInviteItem(finalInviteList, approvedJob.jobID);77 if (!findInvite) {78 finalInviteList.push(79 {80 jobID: approvedJob.jobID,81 jobTitle: approvedJob.jobTitle,82 applicants: [approvedJob.applicant],83 applicationList: [approvedJob.applicationID]84 }85 )86 } else {87 findInvite.applicants.push(approvedJob.applicant);88 findInvite.applicationList.push(approvedJob.applicationID);89 }90 });91 console.log('parsed invite list:', finalInviteList);92 d.resolve(finalInviteList);93 }, function error(err) {94 console.log('managerService::getJobApplyCombo Error', err);95 d.reject(err);96 });97 return d.promise;98 },99 __findInviteItem: function (myFinalList, jobID) {100 for (var myFinalItem of myFinalList) {101 if (myFinalItem.jobID === jobID) {102 return myFinalItem;103 }104 }105 return null;106 },107 createPoll: function (poll) {108 var d = $q.defer();109 httpService.postData('api/admin/poll', poll).then(function success(response) {110 console.log('reviewerService poll response', response);111 d.resolve(response.data);112 }, function error(err) {113 console.log('reviewerService::poll error', err);114 d.reject(err);115 });116 return d.promise;117 }118 };119 return self;...
MainRsvpPage.js
Source:MainRsvpPage.js
...26 errorMessage: null,27 rsvp_info: null28 })29 }30 findInvite(formData) {31 fetch("/api/v1/invites/find.json", {32 credentials: 'same-origin',33 method: 'POST',34 body: JSON.stringify(formData),35 headers: {36 'Accept': 'application/json',37 'Content-Type': 'application/json'38 }39 })40 .then ( response => {41 if ( response.ok ) {42 return response;43 } else {44 let errorMessage = `${response.status} (${response.statusText})`;45 let error = new Error(errorMessage);46 throw(error);47 }48 })49 .then ( response => response.json() )50 .then ( response => {51 if (response["error"]) {52 this.setState({53 errorMessage: response["message"],54 password: ""55 })56 } else {57 this.setState({ rsvp_info: response })58 $(document).ready(function() {59 setTimeout(function(){60 $("#myModal").foundation('reveal', 'open');61 }, 0);62 });63 }64 })65 .catch ( error => console.error(`Error in fetch: ${error.message}`) );66 }67 handleSubmit(event) {68 event.preventDefault()69 if (!this.isFormComplete()) {70 this.setState({ errorMessage: "Only a complete form can be submitted." })71 } else {72 this.setState({ errorMessage: null })73 const formData = {74 invite: {75 first_name: this.state.firstName,76 last_name: this.state.lastName,77 password: this.state.password,78 email: this.state.email79 }80 }81 this.findInvite(formData)82 }83 }84 handleTextChange(event) {85 this.setState({86 [event.target.className]: event.target.value,87 errorMessage: null88 })89 }90 isFormComplete() {91 const requiredFields = [92 this.state.firstName,93 this.state.lastName,94 this.state.password,95 this.state.email...
validateInvite.js
Source:validateInvite.js
1import { respondWithWarning } from '../helpers/responseHandler';2import { fetchOneInvite } from '../services/inviteServices';3/**4 * Function to check if a invite ID is valid5 * @param {Object} req this is the request object6 * @param {Object} res this is the response object7 * @param {Function} next this is the next function8 * @returns {Function} response9 */10export const validateInvite = async (req, res, next) => {11 const { inviteId } = req.params;12 const findInvite = await fetchOneInvite({ inviteId });13 if (!findInvite) {14 return respondWithWarning(res, 404, 'Job Invite not found');15 }16 req.invite = findInvite;17 return next();18};19/**20 * Function to check if a invite is owned by user or user is admin is valid21 * @param {Object} req this is the request object22 * @param {Object} res this is the response object23 * @param {Function} next this is the next function24 * @returns {Function} response25 */26export const validateInviteOwner = async (req, res, next) => {27 if (req.auth.userId !== req.invite.userId && !req.auth.isAdmin) {28 return respondWithWarning(res, 401, 'You are not authorized to perform this action');29 }30 return next();...
Using AI Code Generation
1const { findInvite } = require("qawolf");2const { findInvite } = require("qawolf");3const { findInvite } = require("qawolf");4const { findInvite } = require("qawolf");5const { findInvite } = require("qawolf");6const { findInvite } = require("qawolf");7const { findInvite } = require("qawolf");8const { findInvite } = require("qawolf");9const { findInvite } = require("qawolf");10const { findInvite } = require("qawolf");11const { findInvite } = require("qawolf");12const { findInvite } = require("qawolf");13const { findInvite } = require("qawolf");14const { findInvite } = require("qawolf");15const { findInvite } = require("qawolf");16const { findInvite } = require("qawolf");17const { findInvite } = require("qawolf");18const { findInvite } = require("qawolf");19const { findInvite } = require("qawolf");
Using AI Code Generation
1const { findInvite } = require("qawolf");2const invite = await findInvite();3console.log(invite);4const { create } = require("qawolf");5const browser = await create();6await browser.close();7const { create } = require("qawolf");8const browser = await create();9await browser.close();10const { create } = require("qawolf");11const browser = await create();12await browser.close();13const { create } = require("qawolf");14const browser = await create();15await browser.close();16const { create } = require("qawolf");17const browser = await create();18await browser.close();19const { create } = require("qawolf");20const browser = await create();21await browser.close();22const { create } = require("qawolf");23const browser = await create();24await browser.close();25const { create } = require("qawolf");26const browser = await create();27await browser.close();28const { create } = require("qawolf");29const browser = await create();30await browser.close();31const { create } = require("qawolf");32const browser = await create();33await browser.close();34const { create } = require("qawolf");35const browser = await create();36await browser.close();37const { create } = require("qawolf");38const browser = await create();39await browser.close();40const {
Using AI Code Generation
1const { findInvite } = require("@qawolf/web");2(async () => {3 const invite = await findInvite();4 console.log(invite);5})();6const { findInvite } = require("@qawolf/web");7(async () => {8 const invite = await findInvite();9 console.log(invite);10})();11const { findInvite } = require("@qawolf/web");12(async () => {13 const invite = await findInvite();14 console.log(invite);15})();16const { findInvite } = require("@qawolf/web");17(async () => {18 const invite = await findInvite();19 console.log(invive);20})();21const { findInvite } = require("@qawolf/web");22(async () => {23 const invite = await findInvite();24 console.log(invite);25})();26const { findInvite } = require("@qawolf/web");27(async () => {28 const invite = await findInvite();29 console.log(invite);30})();31const { findInvite } = require("@qawolf/web");32(async () => {33 const invite = await findInvite();34 console.log(invite);35})();36const { findInvite } = require("@qawolf/web");37(async () => {38 const invite = await findInvite();39 console.log(invite);40})();41const { findInvite } = require("@qawolf/web");42(async () => {43 const invite = await findInvite();44 console.log(invite);45})();46const { findInvite } = require("@qawolf/web");47(async () => {48 const invite = await findInvite();49 console.log(invite);50})();
Using AI Code Generation
1const qawolf = require("qawolf");2const { findInvite } = require("qawolf");3const { findInvite } = require("qawolf");4const qawolf = require("qawolf");5const { findInvite } = require("qawolf");6const { findInvite } = require("qawolf");7const qawolf = require("qawolf");8const { findInvite } = require("qawolf");9const { findInvite } = require("qawolf");10const qawolf = require("qawolf");11const { findInvite } = require("qawolf");12const { findInvite } = require("qawolf");13const qawolf = require("qawolf");14const { findInvite } = require("qawolf");15const { findInvite } = require("qawolf");16const qawolf = require("qawolf");17const { findInvite } = require("qawolf");18const { findInvite } = require("qawolf");19const qawolf = require("qawolf");20const { findInvite } = require("qawolf");21const { findInvite } = require("qawolf");22const qawolf = require("qawolf");23const { findInvite } = require("qawolf");24const { findInvite } = require("qawolf");25const qawolf = require("qawolf");26const { findInvite } = require("qawolf");27const { findInvite } = require("qawolf");28const qawolf = require("qawolf");29const { findInvite } = require("qawolf");30const { findInvite } = require("qawolf");31const qawolf = require("qawolf");32const { findInvite } = require("qawolf");33const { findInvite } =
Using AI Code Generation
1const { findInvite } = require('qawolf');2const qawolf = require('qawolf');3const chromium = require('chrome-aws-lambda');4const browser = await chromium.puppeteer.launch({5 });6const browser = await qawolf.launch();7const context = await qawolf.createContext(browser);8const page = await qawolf.createPage(context);9await qawolf.launch();10await qawolf.launch({headless: false});11await qawolf.launch({slowMo: 100});12await qawolf.launch({slowMo: 100, headless: false});13await qawolf.launch({ slowMo: 100, headless: false, executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' });14const context = await qawolf.createContext(browser);15const page = await qawolf.createPage(context);16const page = await qawolf.createPage(context);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!