Created
October 8, 2018 05:58
-
-
Save scorredoira/a445bc287f9b3579ac7db9cf2b2f91d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "stdlib/native" | |
import * as web from "stdlib/web"; | |
export function getPluginInfo() { | |
return { | |
name: "@@Buscador", | |
description: "@@Añade un buscador global en la barra superior." | |
} | |
} | |
function init() { | |
web.addRoute("/amura/search/search.api", searchHandler, web.adminFilter) | |
} | |
function searchHandler(c: web.Context) { | |
let search = c.request.string("search") | |
if (!search) { | |
return; | |
} | |
search = "%" + search + "%"; | |
let chan: async.Channel; | |
let searchFuncs: Function[] = [] | |
let plugins = runtime.context.getPlugins(); | |
searchFuncs.push(searchMenu); | |
if (plugins.contains("amura.crm")) { | |
searchFuncs.push(searchClients); | |
searchFuncs.push(searchClientGroups); | |
} | |
if (plugins.contains("amura.bookings")) { | |
searchFuncs.push(searchReservationType); | |
searchFuncs.push(searchResourceTypes); | |
searchFuncs.push(searchBookingsView); | |
} | |
if (plugins.contains("amura.billing")) { | |
searchFuncs.push(searchProducts); | |
searchFuncs.push(searchPrices); | |
searchFuncs.push(searchVoucherType); | |
searchFuncs.push(searchMembership); | |
searchFuncs.push(searchCompany); | |
} | |
const jobCount = searchFuncs.length; | |
// make a buffered channel so it doesn't block | |
chan = async.newChannel(jobCount); | |
// run all jobs | |
let wg = async.newWaitGroup(); | |
for (let i = 0, l = jobCount; i < l; i++) { | |
wg.run(() => searchFuncs[i](chan, search)) | |
} | |
wg.wait(); | |
// get all results | |
let result: any[] = []; | |
for (let i = 0; i < jobCount; i++) { | |
let items = chan.receive(); | |
result.pushRange(items) | |
} | |
c.response.writeJSON(result) | |
} | |
function searchVoucherType(chan: async.Channel, search: string) { | |
let items = sql.query(`SELECT id, name | |
FROM amura:billing:vouchertype | |
WHERE name LIKE ? | |
ORDER BY name | |
LIMIT 30`, search) | |
let section = T("@@Tipo de bono") + ": "; | |
chan.send(items.select(t => { | |
return { | |
icon: "settings", | |
url: "/admin/amura/billing/vouchertype/" + t.id, | |
id: t.id, | |
text: section + t.name | |
} | |
})) | |
} | |
function searchMembership(chan: async.Channel, search: string) { | |
let items = sql.query(`SELECT id, name | |
FROM amura:billing:membership | |
WHERE name LIKE ? | |
ORDER BY name | |
LIMIT 30`, search) | |
let section = T("@@Cuota") + ": "; | |
chan.send(items.select(t => { | |
return { | |
icon: "settings", | |
url: "/admin/amura/billing/membership/" + t.id, | |
id: t.id, | |
text: section + t.name | |
} | |
})) | |
} | |
function searchCompany(chan: async.Channel, search: string) { | |
let items = sql.query(`SELECT id, name | |
FROM amura:billing:company | |
WHERE name LIKE ? | |
ORDER BY name | |
LIMIT 30`, search) | |
let section = T("@@Empresa") + ": "; | |
chan.send(items.select(t => { | |
return { | |
icon: "settings", | |
url: "/admin/amura/billing/company/" + t.id, | |
id: t.id, | |
text: section + t.name | |
} | |
})) | |
} | |
function searchPrices(chan: async.Channel, search: string) { | |
let items = sql.query(`SELECT id, name | |
FROM amura:billing:price | |
WHERE name LIKE ? | |
ORDER BY name | |
LIMIT 30`, search) | |
let section = T("@@Precio") + ": "; | |
chan.send(items.select(t => { | |
return { | |
icon: "settings", | |
url: "/admin/amura/billing/price/" + t.id, | |
id: t.id, | |
text: section + t.name | |
} | |
})) | |
} | |
function searchProducts(chan: async.Channel, search: string) { | |
let items = sql.query(`SELECT id, name | |
FROM amura:billing:product | |
WHERE name LIKE ? | |
ORDER BY name | |
LIMIT 30`, search) | |
let section = T("@@Producto") + ": "; | |
chan.send(items.select(t => { | |
return { | |
icon: "settings", | |
url: "/admin/amura/billing/product/" + t.id, | |
id: t.id, | |
text: section + t.name | |
} | |
})) | |
} | |
function searchMenu(chan: async.Channel, search: string) { | |
let items = sql.query(`SELECT id, label , url | |
FROM amura:system:menu | |
WHERE label LIKE ? | |
AND url IS NOT NULL | |
ORDER BY label | |
LIMIT 30`, search) | |
let section = T("@@Menu") + ": "; | |
chan.send(items.select(t => { | |
return { | |
icon: "menu", | |
url: t.url, | |
id: t.id, | |
text: section + T(t.label) | |
} | |
})) | |
} | |
function searchBookingsView(chan: async.Channel, search: string) { | |
let items = sql.query(`SELECT id, name | |
FROM amura:bookings:view | |
WHERE name LIKE ? | |
ORDER BY name | |
LIMIT 30`, search) | |
let section = T("@@Vista de ocupación") + ": "; | |
chan.send(items.select(t => { | |
return { | |
icon: "settings", | |
url: "/admin/amura/bookings/view/" + t.id, | |
id: t.id, | |
text: section + t.name | |
} | |
})) | |
} | |
function searchClientGroups(chan: async.Channel, search: string) { | |
let items = sql.query(`SELECT id, name | |
FROM amura:crm:clientgroup | |
WHERE name LIKE ? | |
ORDER BY name | |
LIMIT 30`, search) | |
let section = T("@@Grupo de clientes") + ": "; | |
chan.send(items.select(t => { | |
return { | |
icon: "settings", | |
url: "/admin/amura/crm/clientgroup/" + t.id, | |
id: t.id, | |
text: section + t.name | |
} | |
})) | |
} | |
function searchResourceTypes(chan: async.Channel, search: string) { | |
let items = sql.query(`SELECT id, name | |
FROM amura:bookings:resourcetype | |
WHERE name LIKE ? | |
ORDER BY name | |
LIMIT 30`, search) | |
let section = T("@@Tipos de recurso") + ": "; | |
chan.send(items.select(t => { | |
return { | |
icon: "settings", | |
url: "/admin/amura/bookings/resourcetype/" + t.id, | |
id: t.id, | |
text: section + t.name | |
} | |
})) | |
} | |
function searchReservationType(chan: async.Channel, search: string) { | |
let items = sql.query(`SELECT id, name | |
FROM amura:bookings:type | |
WHERE name LIKE ? | |
ORDER BY name | |
LIMIT 30`, search) | |
let section = T("@@Tipo de reserva") + ": "; | |
chan.send(items.select(t => { | |
return { | |
icon: "settings", | |
url: "/admin/amura/bookings/type/" + t.id, | |
id: t.id, | |
text: section + t.name | |
} | |
})) | |
} | |
function searchClients(chan: async.Channel, search: string) { | |
let items = sql.query(`SELECT c.id, | |
CONCAT_WS(", ", c.lastName, c.name) AS fullName, | |
( | |
SELECT GROUP_CONCAT(b.tag) | |
FROM amura:billing:membershipclient m | |
JOIN amura:billing:membership b ON m.idMembership = b.id | |
WHERE idclient = c.id | |
AND b.tag IS NOT NULL | |
AND m.start < now() | |
AND (m.end IS NULL OR m.end > now()) | |
) as tags | |
FROM amura:crm:client c | |
WHERE c.name LIKE ? | |
OR c.lastName LIKE ? | |
OR c.phone LIKE ? | |
OR c.email LIKE ? | |
OR c.nationalID LIKE ? | |
OR c.centerCard LIKE ? | |
ORDER BY fullName | |
LIMIT 30`, search, search, search, search, search, search); | |
let section = T("@@Cliente") + ": "; | |
chan.send(items.select(t => { | |
return { | |
icon: "client", | |
url: "/admin/amura/crm/client/" + t.id, | |
id: t.id, | |
text: section + t.fullName, | |
tags: t.tags, | |
} | |
})) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment