Added filter
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2020-10-05 07:53:03 +02:00
parent a2232f659b
commit cdde4d95d7
11 changed files with 223 additions and 5 deletions

View File

@ -0,0 +1,41 @@
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
module.exports = Self => {
Self.remoteMethod('latest', {
description: 'Returns the lastest campaigns',
accessType: 'READ',
accepts: [{
arg: 'filter',
type: 'Object',
description: `Filter defining where, order, offset, and limit - must be a JSON-encoded string`
}],
returns: {
type: ['object'],
root: true
},
http: {
path: `/latest`,
verb: 'GET'
}
});
Self.latest = async filter => {
const conn = Self.dataSource.connector;
const minDate = new Date();
minDate.setFullYear(minDate.getFullYear() - 1);
minDate.setMonth(0);
minDate.setDate(1);
const where = {dated: {gte: minDate}};
filter = mergeFilters(filter, {where});
const stmt = new ParameterizedSQL(
`SELECT * FROM campaign`);
stmt.merge(conn.makeWhere(filter.where));
stmt.merge('GROUP BY code');
stmt.merge(conn.makePagination(filter));
return conn.executeStmt(stmt);
};
};

View File

@ -0,0 +1,30 @@
module.exports = Self => {
Self.remoteMethod('upcoming', {
description: 'Returns the lastest campaigns',
accessType: 'READ',
accepts: [],
returns: {
type: ['object'],
root: true
},
http: {
path: `/upcoming`,
verb: 'GET'
}
});
Self.upcoming = async() => {
const minDate = new Date();
minDate.setMonth(0);
minDate.setDate(1);
return Self.findOne({
where: {
dated: {
lt: minDate
}
},
order: 'dated DESC'
});
};
};

View File

@ -8,6 +8,9 @@
"Bank": {
"dataSource": "vn"
},
"Campaign": {
"dataSource": "vn"
},
"Country": {
"dataSource": "vn"
},

4
back/models/campaign.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = Self => {
require('../methods/campaign/latest')(Self);
require('../methods/campaign/upcoming')(Self);
};

33
back/models/campaign.json Normal file
View File

@ -0,0 +1,33 @@
{
"name": "Campaign",
"base": "VnModel",
"options": {
"mysql": {
"table": "campaign"
}
},
"properties": {
"id": {
"type": "number",
"required": true
},
"code": {
"type": "string",
"required": true
},
"dated": {
"type": "date"
},
"scopeDays": {
"type": "number"
}
},
"acls": [
{
"accessType": "READ",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW"
}
]
}

View File

@ -0,0 +1,37 @@
CREATE TABLE `vn`.campaign
(
id INT AUTO_INCREMENT,
code ENUM('mothersDay', 'allSaints', 'valentinesDay') NOT NULL,
dated DATE DEFAULT CURDATE() NOT NULL,
scopeDays INT NOT NULL DEFAULT '15',
CONSTRAINT campaign_pk
PRIMARY KEY (id)
);
CREATE UNIQUE INDEX campaign_dated_uindex
ON `vn`.campaign (dated);
-- TODOS SANTOS
INSERT INTO `vn`.campaign(code, dated)
SELECT 'allSaints' AS code, dated
FROM `vn`.time
WHERE dated >= CONCAT(YEAR(CURDATE()) - 1, '-01-01')
AND month = 11
AND day = 1;
-- SAN VALENTIN
INSERT INTO `vn`.campaign(code, dated)
SELECT 'valentinesDay' AS code, dated
FROM `vn`.time
WHERE dated >= CONCAT(YEAR(CURDATE()) - 1, '-01-01')
AND month = 2
AND day = 14;
-- DIA DE LA MADRE
INSERT INTO `vn`.campaign(code, dated)
SELECT 'mothersDay' AS code, dated
FROM `vn`.time
WHERE dated >= CONCAT(YEAR(CURDATE()) - 1, '-01-01')
AND month = 5
AND WEEK(dated, 5) - WEEK(DATE_SUB(dated, INTERVAL DAYOFMONTH(dated) - 1 DAY), 5) + 1 = 1 -- WEEK OF MONTH
AND DAYOFWEEK(dated) = 1;

View File

@ -190,9 +190,27 @@ export default class Autocomplete extends Field {
this.input.value = display;
if (this.translateFields) {
if (this.translateFields.indexOf(this.showField) > -1)
this.input.value = this.$t(display);
if (this.translateFields && this.selection) {
const translations = [];
for (let field of this.translateFields) {
const fieldValue = this._selection[field];
translations.push({
original: fieldValue,
value: this.$t(fieldValue)
});
}
for (let translation of translations) {
const orgValue = translation.original;
const value = translation.value;
display = display.replace(orgValue, value);
}
this.input.value = display;
/* if (this.translateFields.indexOf(this.showField) > -1)
this.input.value = this.$t(display); */
}
}

View File

@ -49,6 +49,22 @@
ng-model="filter.categoryId">
</vn-autocomplete>
</vn-horizontal>
<vn-horizontal>
<vn-autocomplete vn-one
url="Campaigns/latest"
label="Campaign"
translate-fields="['code']"
show-field="code"
value-field="id"
ng-model="filter.campaign"
order="dated DESC"
selection="$ctrl.campaignSelection"
search-function="{or: [{dated: {like: '%'+ $search +'%'}}]}">
<tpl-item>
{{code}} {{dated | date: 'yyyy'}}
</tpl-item>
</vn-autocomplete>
</vn-horizontal>
<vn-horizontal>
<vn-date-picker
vn-one

View File

@ -1,7 +1,36 @@
import ngModule from '../module';
import SearchPanel from 'core/components/searchbar/search-panel';
class Controller extends SearchPanel {
constructor($, $element) {
super($, $element);
this.$http.get('Campaigns/upcoming').then(res => {
const filter = this.$.filter;
filter.campaign = res.data.id;
console.log(res.data);
});
}
get campaignSelection() {
return this._campaignSelection;
}
set campaignSelection(value) {
this._campaignSelection = value;
if (!value) return;
const filter = this.$.filter;
const from = new Date(value.dated);
from.setDate(from.getDate() - value.scopeDays);
filter.to = value.dated;
filter.from = from;
}
}
ngModule.vnComponent('vnConsumptionSearchPanel', {
template: require('./index.html'),
controller: SearchPanel
controller: Controller
});

View File

@ -0,0 +1,3 @@
allSaints: All Saints Day
valentinesDay: Valentine's Day
mothersDay: Mother's day

View File

@ -1,3 +1,7 @@
Item id: Id artículo
From: Desde
To: Hasta
Campaign: Campaña
allSaints: Día de todos los Santos
valentinesDay: Día de San Valentín
mothersDay: Día de la madre