allow inline locations

This commit is contained in:
Florian Dold 2020-09-05 18:09:48 +05:30
parent ccaac89c3b
commit e0c10a06b1
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
2 changed files with 106 additions and 18 deletions

View File

@ -265,10 +265,45 @@ export class AuditorHandle {
url: string; url: string;
} }
// Delivery location, losely modeled as a subset of
// ISO20022's PostalAddress25.
export interface Location {
// Nation with its own government.
country?: string;
// Identifies a subdivision of a country such as state, region, county.
country_subdivision?: string;
// Identifies a subdivision within a country sub-division.
district?: string;
// Name of a built-up area, with defined boundaries, and a local government.
town?: string;
// Specific location name within the town.
town_location?: string;
// Identifier consisting of a group of letters and/or numbers that
// is added to a postal address to assist the sorting of mail.
post_code?: string;
// Name of a street or thoroughfare.
street?: string;
// Name of the building or house.
building_name?: string;
// Number that identifies the position of a building on a street.
building_number?: string;
// Free-form address lines, should not exceed 7 elements.
address_lines?: string[];
}
export interface MerchantInfo { export interface MerchantInfo {
name: string; name: string;
jurisdiction: string | undefined; jurisdiction?: Location;
address: string | undefined; address?: Location;
} }
export interface Tax { export interface Tax {
@ -306,12 +341,6 @@ export interface Product {
// time indicating when this product should be delivered // time indicating when this product should be delivered
delivery_date?: Timestamp; delivery_date?: Timestamp;
// where to deliver this product. This may be an URL for online delivery
// (i.e. 'http://example.com/download' or 'mailto:customer@example.com'),
// or a location label defined inside the proposition's 'locations'.
// The presence of a colon (':') indicates the use of an URL.
delivery_location?: string;
} }
export interface InternationalizedString { export interface InternationalizedString {
@ -364,11 +393,6 @@ export class ContractTerms {
*/ */
pay_deadline: Timestamp; pay_deadline: Timestamp;
/**
* Delivery locations.
*/
locations: any;
/** /**
* Maximum deposit fee covered by the merchant. * Maximum deposit fee covered by the merchant.
*/ */
@ -384,6 +408,17 @@ export class ContractTerms {
*/ */
merchant_pub: string; merchant_pub: string;
/**
* Time indicating when the order should be delivered.
* May be overwritten by individual products.
*/
delivery_date?: Timestamp;
/**
* Delivery location for (all!) products.
*/
delivery_location?: Location;
/** /**
* List of accepted exchanges. * List of accepted exchanges.
*/ */
@ -1035,11 +1070,26 @@ export const codecForAuditorHandle = (): Codec<AuditorHandle> =>
.property("url", codecForString()) .property("url", codecForString())
.build("AuditorHandle"); .build("AuditorHandle");
export const codecForLocation = (): Codec<Location> =>
buildCodecForObject<Location>()
.property("country", codecOptional(codecForString()))
.property("country_subdivision", codecOptional(codecForString()))
.property("building_name", codecOptional(codecForString()))
.property("building_number", codecOptional(codecForString()))
.property("district", codecOptional(codecForString()))
.property("street", codecOptional(codecForString()))
.property("post_code", codecOptional(codecForString()))
.property("town", codecOptional(codecForString()))
.property("town_location", codecOptional(codecForString()))
.property("address_lines", codecOptional(codecForList(codecForString())))
.build("Location");
export const codecForMerchantInfo = (): Codec<MerchantInfo> => export const codecForMerchantInfo = (): Codec<MerchantInfo> =>
buildCodecForObject<MerchantInfo>() buildCodecForObject<MerchantInfo>()
.property("name", codecForString()) .property("name", codecForString())
.property("address", codecOptional(codecForString())) .property("address", codecOptional(codecForLocation()))
.property("jurisdiction", codecOptional(codecForString())) .property("jurisdiction", codecOptional(codecForLocation()))
.build("MerchantInfo"); .build("MerchantInfo");
export const codecForTax = (): Codec<Tax> => export const codecForTax = (): Codec<Tax> =>
@ -1063,8 +1113,6 @@ export const codecForProduct = (): Codec<Product> =>
.property("quantity", codecOptional(codecForNumber())) .property("quantity", codecOptional(codecForNumber()))
.property("unit", codecOptional(codecForString())) .property("unit", codecOptional(codecForString()))
.property("price", codecOptional(codecForString())) .property("price", codecOptional(codecForString()))
.property("delivery_date", codecOptional(codecForTimestamp))
.property("delivery_location", codecOptional(codecForString()))
.build("Tax"); .build("Tax");
export const codecForContractTerms = (): Codec<ContractTerms> => export const codecForContractTerms = (): Codec<ContractTerms> =>
@ -1089,7 +1137,8 @@ export const codecForContractTerms = (): Codec<ContractTerms> =>
.property("refund_deadline", codecForTimestamp) .property("refund_deadline", codecForTimestamp)
.property("wire_transfer_deadline", codecForTimestamp) .property("wire_transfer_deadline", codecForTimestamp)
.property("timestamp", codecForTimestamp) .property("timestamp", codecForTimestamp)
.property("locations", codecForAny()) .property("delivery_location", codecOptional(codecForLocation()))
.property("delivery_date", codecOptional(codecForTimestamp))
.property("max_fee", codecForString()) .property("max_fee", codecForString())
.property("max_wire_fee", codecOptional(codecForString())) .property("max_wire_fee", codecOptional(codecForString()))
.property("merchant", codecForMerchantInfo()) .property("merchant", codecForMerchantInfo())

View File

@ -53,3 +53,42 @@ test("contract terms validation", (t) => {
t.fail(); t.fail();
}); });
test("contract terms validation (locations)", (t) => {
const c = {
nonce: "123123123",
h_wire: "123",
amount: "EUR:1.5",
auditors: [],
exchanges: [{ master_pub: "foo", url: "foo" }],
fulfillment_url: "foo",
max_fee: "EUR:1.5",
merchant_pub: "12345",
merchant: {
name: "Foo",
address: {
country: "DE",
}
},
order_id: "test_order",
pay_deadline: { t_ms: 42 },
wire_transfer_deadline: { t_ms: 42 },
merchant_base_url: "https://example.com/pay",
products: [],
refund_deadline: { t_ms: 42 },
summary: "hello",
timestamp: { t_ms: 42 },
wire_method: "test",
delivery_location: {
country: "FR",
town: "Rennes",
}
};
const r = codecForContractTerms().decode(c);
t.assert(r.merchant.address?.country === "DE");
t.assert(r.delivery_location?.country === "FR");
t.assert(r.delivery_location?.town === "Rennes");
});