Advertisement
Guest User

Untitled

a guest
Oct 13th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. import * as mssql from 'mssql';
  2. import { AppConfig } from '../config';
  3. import { ActionService } from './data-services';
  4. import { Logger, LoggerFactory } from '../common';
  5.  
  6. export class AppDataServices {
  7. private static readonly LOGGER: Logger = LoggerFactory.getLogger();
  8.  
  9. private db: any;
  10.  
  11. public actionService: ActionService;
  12.  
  13. constructor(private appConfig: AppConfig) {
  14. this.initConnectionPool();
  15. this.actionService = new ActionService(this.db, AppDataServices.LOGGER);
  16. }
  17.  
  18. private initConnectionPool() {
  19. this.db = new mssql.ConnectionPool({
  20. user: this.appConfig.mssqlUsername,
  21. password: this.appConfig.mssqlPassword,
  22. server: this.appConfig.mssqlServer,
  23. database: this.appConfig.mssqlDatabase,
  24. // If you are on Microsoft Azure, you need this:
  25. options: { encrypt: true }
  26. }, (err: any) => {
  27. if (err) AppDataServices.LOGGER.error('MSSQL error', err);
  28. });
  29. }
  30. }
  31.  
  32. import * as mssql from 'mssql';
  33. import { Logger, LoggerFactory } from '../../../common';
  34.  
  35. export class ActionService {
  36. private static readonly LOGGER: Logger = LoggerFactory.getLogger();
  37.  
  38. constructor (private db: any, private logger: any) {
  39. this.insertion();
  40. }
  41.  
  42. insertion() {
  43. const request = new mssql.Request(this.db);
  44. request.query(
  45. `
  46. INSERT INTO dbo.action
  47. (timestamp, result, description, request_endpoint, request_payload, response_status, response_payload, actualAmount, rule_id, lendbook_id)
  48. SELECT t1.start, 'SUCCESS', NULL, '/lendbook/usd', NULL, '200', NULL, t1.originalAmount, t1.id, t2.id
  49. FROM dbo.rule t1, dbo.lendbook t2
  50. INNER JOIN t2.id = t1.id
  51. `, (err, result) => {
  52. if (err) {
  53. console.error(err);
  54. return;
  55. }
  56. return result.recordsets[0];
  57. });
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement