ticket #3 - Added learning cartridge with all complete examples

This commit is contained in:
Max Gialanella
2021-12-22 11:09:55 -07:00
parent 8f15cd15df
commit 3f354195f2
33 changed files with 783 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
'use strict';
var server = require('server');
var cyborgForm = server.forms.getForm('cyborgregistrationform');
var Logger = require('dw/system/Logger');
var log = Logger.getLogger('DevTest','Cyborg');
server.get('Start', function (req, res, next) {
cyborgForm.clear();
log.info('Form loaded.');
res.render('cyborgTracker/registrationform', {
cyborgForm: cyborgForm,
disabled: false
});
next();
});
server.post('HandleForm', function (req, res, next) {
var storageService = require('~/cartridge/scripts/storageService');
var Transaction = require('dw/system/Transaction');
var message = '';
log.info('Cyborg form handler loaded.');
Transaction.begin();
try {
var co = storageService.storeCyborgForm(cyborgForm);
Transaction.commit();
message = 'Form Saved';
log.info('Cyborg form saved');
}
catch (e){
Transaction.rollback();
message = 'Failed to save';
log.warn('Cyborg form failed to save ' + e);
}
res.render('cyborgTracker/registrationform', {
cyborgForm: cyborgForm,
message: message,
disabled: true
});
next();
});
module.exports = server.exports();

View File

@@ -0,0 +1,43 @@
'use strict';
var server = require('server');
var cache = require('*/cartridge/scripts/middleware/cache');
var ProductFactory = require('*/cartridge/scripts/factories/product');
var StoreMgr = require('dw/catalog/StoreMgr');
var Logger = require('dw/system/Logger');
var log = Logger.getLogger('DevTest','Cyborg');
server.get('Start',
cache.applyShortPromotionSensitiveCache,
function (req, res, next) {
var params = {'pid':'sony-kdl-55xbr8M'};
var productModel = ProductFactory.get(params);
var storeModel = StoreMgr.getStore('store1');
var viewData = {
'productModel': productModel,
'storeModel': storeModel
};
log.debug('Loaded demo page');
res.render('demo', viewData);
next();
});
server.get('Designer', function (req, res, next) {
let PageMgr = require('dw/experience/PageMgr');
let page = PageMgr.getPage('testpage');
log.warn('Loaded page designer page');
res.print(PageMgr.renderPage(page.ID, 'testpage'));
next();
});
module.exports = server.exports();

View File

@@ -0,0 +1,22 @@
'use strict';
var server = require('server');
server.get('Start', function (req, res, next) {
res.print('Greetings Planet');
next();
});
server.get('Showme', function (req, res, next) {
// Use template hello.isml
// Pass data to pdict using name param1
res.render('hello', { 'param1':'This will render.' });
next();
});
module.exports = server.exports();

View File

@@ -0,0 +1,44 @@
'use strict';
var server = require('server');
//Use super module to extend existing home route
server.extend(module.superModule);
//import userLoggedIn middleware
var userLoggedIn = require('*/cartridge/scripts/middleware/userLoggedIn');
//Use prepend to check for logged in user
server.prepend('Show', userLoggedIn.validateLoggedIn, function (req, res, next) {
var viewData = res.getViewData();
viewData.detailText = 'Please log in'; //One method to set view data
if (req.currentCustomer.profile) {
viewData.detailText = 'Welcome ' + customer.getProfile().getFirstName();
}
next();
});
//Use append to check for promotion
server.append('Show', function (req, res, next) {
var promoText = 'There are no Promotions at this time';
//read query string to check for fromotions
if (req.querystring.promo == 1) {
promoText = 'All Electronics are 98% off! Practically free!';
}
if (req.querystring.promo == 2) {
promoText = 'Overnight shipping is free!';
}
res.setViewData(
{ promoText: promoText } //Another way to set view data
);
next();
});
module.exports = server.exports();

View File

@@ -0,0 +1,79 @@
'use strict';
var server = require('server');
var csrfProtection = require('*/cartridge/scripts/middleware/csrf');
var userLoggedIn = require('*/cartridge/scripts/middleware/userLoggedIn');
var consentTracking = require('*/cartridge/scripts/middleware/consentTracking');
var Logger = require('dw/system/Logger');
var log = Logger.getLogger('DevTest','Notifications');
server.get('Start',
server.middleware.https,
userLoggedIn.validateLoggedIn,
consentTracking.consent,
csrfProtection.generateToken,
function (req, res, next) {
var notificationsForm = server.forms.getForm('notifications');
notificationsForm.clear();
if(customer.profile.custom.notificationSales){
notificationsForm.notificationSales.checked = 'checked';
}
if(customer.profile.custom.notificationNew){
notificationsForm.notificationNew.checked = 'checked';
}
if(customer.profile.custom.notificationStock){
notificationsForm.notificationStock.checked = 'checked';
}
res.render('notificationsform', {
notificationsForm: notificationsForm
});
log.debug('Rendered Notifications Form');
next();
});
server.post('HandleForm',
server.middleware.https,
csrfProtection.validateRequest,
function (req, res, next) {
var notificationsForm = server.forms.getForm('notifications');
var URLUtils = require('dw/web/URLUtils');
var Transaction = require('dw/system/Transaction');
Transaction.begin();
try {
customer.profile.custom.notificationSales = notificationsForm.notificationSales.value
customer.profile.custom.notificationNew = notificationsForm.notificationNew.value
customer.profile.custom.notificationStock = notificationsForm.notificationStock.value
Transaction.commit();
log.error('Saved notifications');
var HookMgr = require('dw/system/HookMgr');
var userEmail = customer.getProfile().getEmail();
HookMgr.callHook('app.notification.email', 'sendNotificationsChange', userEmail);
} catch (error) {
Transaction.rollback();
log.error(error);
}
// res.redirect( URLUtils.url('Notifications') ); //For testing
res.redirect( URLUtils.url('Account-Show') );
next();
})
module.exports = server.exports();

View File

@@ -0,0 +1,19 @@
'use strict';
var server = require('server');
//Basket Manager will get all the users basket information
var BasketMgr = require('dw/order/BasketMgr');
server.get('List', function (req, res, next) {
var viewData = {
'basket': BasketMgr.getCurrentOrNewBasket()
};
res.render('simplebasket', viewData);
next();
});
module.exports = server.exports();