Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ashward committed Jul 26, 2024
1 parent 2cf6565 commit 5e8483d
Show file tree
Hide file tree
Showing 20 changed files with 29,835 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SEARCH_API_BASE=https://search.dev.trade-tariff.service.gov.uk
SEARCH_API_SECRET_KEY=
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Node.js ignores
node_modules/

# Prototype ignores - per-user
.tmp/
.env
migrate.log
usage-data-config.json

# General ignores
.DS_Store
.idea
21 changes: 21 additions & 0 deletions LICENCE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (C) 2014 Crown Copyright (Government Digital Service)

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
8 changes: 8 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// For guidance on how to add JavaScript see:
// https://prototype-kit.service.gov.uk/docs/adding-css-javascript-and-images
//

window.GOVUKPrototypeKit.documentReady(() => {
// Add JavaScript here
})
6 changes: 6 additions & 0 deletions app/assets/sass/application.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//
// For guidance on how to add CSS and SCSS see:
// https://prototype-kit.service.gov.uk/docs/adding-css-javascript-and-images
//

// Add extra styles here
3 changes: 3 additions & 0 deletions app/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"serviceName": "Commodi-tea ☕"
}
25,534 changes: 25,534 additions & 0 deletions app/data/commodities/commodities.csv

Large diffs are not rendered by default.

62 changes: 62 additions & 0 deletions app/data/commodities/commodities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const { parse } = require('csv-parse/sync');
const fs = require('fs');

const rawCommodities = parse(fs.readFileSync(__dirname + `/commodities.csv`), {
columns: true,
skip_empty_lines: true
})
"SID","Commodity code","Product line suffix","Start date","End date","Indentation","End line","Description","Class","ItemIDPlusPLS","Hierarchy"
const commodities = {}

for(rawCode of rawCommodities) {
const newCode = {
code: rawCode["Commodity code"],
suffix: rawCode["Product line suffix"],
description: rawCode["Description"],
hierarchy: rawCode["Hierarchy"].split(',').map(ancestor => {
const [ancestor_code, ancestor_suffix] = ancestor.split('_');
return { code: ancestor_code, suffix: ancestor_suffix }
})
}

if(commodities[rawCode["Commodity code"]]) {
commodities[rawCode["Commodity code"]].push(newCode);
} else {
commodities[rawCode["Commodity code"]] = [newCode];
}
}

const findSubheading = (code, suffix) => {
return findCommodity(`${code}`.padEnd(10, '0'), suffix);
}

const findCommodity = (code, suffix) => {
const commodity = commodities[code];

if(suffix) {
console.log(code, suffix, commodity.find(c => c.suffix == suffix))
return commodity.find(c => c.suffix == suffix)
}

if(commodity && commodity.length > 0) {
return commodity[0]
}

return null;
}

const expandHierarchy = (commodity) => {

if(!commodity?.hierarchy)
return []

return commodity.hierarchy
.map(ancestor => findCommodity(ancestor.code, ancestor.suffix))
.filter(ancestor => !!ancestor)
}

module.exports = {
findSubheading,
findCommodity,
expandHierarchy,
}
5 changes: 5 additions & 0 deletions app/data/session-data-defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {

// Insert values here

}
10 changes: 10 additions & 0 deletions app/filters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// For guidance on how to create filters see:
// https://prototype-kit.service.gov.uk/docs/filters
//

const govukPrototypeKit = require('govuk-prototype-kit')
const addFilter = govukPrototypeKit.views.addFilter

// Add your filters here

75 changes: 75 additions & 0 deletions app/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// For guidance on how to create routes see:
// https://prototype-kit.service.gov.uk/docs/create-routes
//
require('dotenv').config();

const fs = require('fs');
const govukPrototypeKit = require('govuk-prototype-kit')
const commodities = require('./data/commodities/commodities');
const { default: axios } = require('axios');

const router = govukPrototypeKit.requests.setupRouter()

const descriptions = fs.readFileSync(`${__dirname}/../descriptions.txt`).toString().split("\n");

const searchApiBase = process.env["SEARCH_API_BASE"] || "http://127.0.0.1:5000";
const searchApiSecretKey = process.env["SEARCH_API_SECRET_KEY"]

// Add your routes here
router.get('/new-product', async (request, response) => {
request.session.data.description = descriptions[Math.floor(Math.random() * descriptions.length)];

const result = await axios.post(`${searchApiBase}/fpo-code-search`, {
description: request.session.data.description,
digits: 8
}, {
headers: {
'X-Api-Key': searchApiSecretKey,
}
}
)

const searchResults = await result.data;

if(searchResults["results"].length > 0) {
const searchResult = searchResults["results"][0];

const commodity = commodities.findSubheading(searchResult.code);

request.session.data.searchResult = {
code: searchResult.code,
score: searchResult.score.toFixed(1),
commodity: commodity,
hierarchy: commodities.expandHierarchy(commodity).filter(c => c.description.toLowerCase() != 'other').reverse(),
}

console.log(request.session.data.searchResult)
} else {
request.session.data.searchResult = null;
}

const result6 = await axios.post(`${searchApiBase}/fpo-code-search`, {
description: request.session.data.description,
digits: 8
}, {
headers: {
'X-Api-Key': searchApiSecretKey,
}
}
)

response.redirect('product')
})

router.post('/product-answer', async (request, response) => {
const isCodeCorrect = request.session.data.isCodeCorrect;

if(isCodeCorrect == 'yes') {
response.redirect('/answer-yes');
} else if(isCodeCorrect == 'maybe') {
response.redirect('/answer-maybe');
} else if(isCodeCorrect == 'no') {
response.redirect('/answer-no');
}
});
72 changes: 72 additions & 0 deletions app/views/answer-maybe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{% extends "layouts/main.html" %}

{% block pageTitle %}
Question page template – {{ serviceName }} – GOV.UK Prototype Kit
{% endblock %}

{% block beforeContent %}
{{ govukBackLink({
text: "Back",
href: "javascript:window.history.back()"
}) }}
{% endblock %}

{% block content %}

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">

<form class="form" action="/path/of/next/page" method="post">
<span class="govuk-caption-l">"{{ data['description'] }}"</span>

{{ govukCheckboxes({
name: "whereDoYouLive",
fieldset: {
legend: {
text: "What extra information would you need to classify this to 8 digits?",
isPageHeading: true,
classes: "govuk-fieldset__legend--l"
}
},
items: [
{
value: "item-type",
text: "Type of item"
},
{
value: "material",
text: "Material"
},
{
value: "purpose",
text: "The purpose of the item"
},
{
value: "state",
text: "What state the item is in"
},
{
value: "alcohol-volume",
text: "Alcohol volume"
},
{
value: "gender",
text: "Gender (e.g. for clothing)"
},
{
value: "container-size",
text: "Container size"
}
]
}) }}

{{ govukButton({
text: "Continue"
}) }}

</form>

</div>
</div>

{% endblock %}
58 changes: 58 additions & 0 deletions app/views/answer-yes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{% extends "layouts/main.html" %}

{% block pageTitle %}
Question page template – {{ serviceName }} – GOV.UK Prototype Kit
{% endblock %}

{% block beforeContent %}
{{ govukBackLink({
text: "Back",
href: "javascript:window.history.back()"
}) }}
{% endblock %}

{% block content %}

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<span class="govuk-caption-l">"{{ data['description'] }}"</span>

<form class="form" action="thanks" method="get">

{{ govukRadios({
name: "whereDoYouLive",
fieldset: {
legend: {
text: "Did you have to refer anywhere else (e.g. web search) to identify it?",
isPageHeading: true,
classes: "govuk-fieldset__legend--l"
}
},
items: [
{
value: "yes",
text: "Yes",
hint: {
text: "I had to refer elsewhere, like a web search for the product"
}
},
{
value: "no",
text: "No",
hint: {
text: "All of the information and context I needed was in the description"
}
}
]
}) }}

{{ govukButton({
text: "Continue"
}) }}

</form>

</div>
</div>

{% endblock %}
Loading

0 comments on commit 5e8483d

Please sign in to comment.