Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ selected variant; add `--all-options` to include the other variants.

| Command | Description |
|---|---|
| `mna options add <tripId> <variantId> <destinationKey> <kind> --from-json=<file> [--free-cancellation-until=<date>] [--details=<json>] [--photos=<json>]` | Add an option from JSON. `--free-cancellation-until`, `--details` and `--photos` (accommodation only) merge into the body. |
| `mna options edit <tripId> <variantId> <destinationKey> <kind> <optionKey> [--from-json=<file>] [--free-cancellation-until=<date>] [--details=<json>] [--photos=<json>]` | Edit an option. For accommodation, each flag works alone, without a JSON file. `--details` changes only the properties it names; `--photos` replaces the list. |
| `mna options add <tripId> <variantId> <destinationKey> <kind> --from-json=<file> [--free-cancellation-until=<date>] [--details=<json>] [--photos=<json>] [--website=<url>]` | Add an option from JSON. `--free-cancellation-until`, `--details` and `--photos` (accommodation only) merge into the body. |
| `mna options edit <tripId> <variantId> <destinationKey> <kind> <optionKey> [--from-json=<file>] [--free-cancellation-until=<date>] [--details=<json>] [--photos=<json>] [--website=<url>]` | Edit an option. For accommodation, each flag works alone, without a JSON file. `--details` changes only the properties it names; `--photos` replaces the list. |
| `mna options delete <tripId> <variantId> <destinationKey> <kind> <optionKey> [--yes]` | Delete an option. |
| `mna options select <tripId> <variantId> <destinationKey> <kind> <optionKey>` | Select an option. |
| `mna options deselect <tripId> <variantId> <destinationKey> <kind>` | Deselect the current option. |
Expand Down
6 changes: 3 additions & 3 deletions skills/mna/references/cli-and-schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ variants add <trip> --name <dates…> [--adults --child-ages --no-children -
destinations add <trip> <var> --place [--start-date --end-date --notes --return-to-home]
edit <trip> <var> <destKey> [--place --notes --start-date --end-date --return-to-home/--no-…]
reorder <trip> <var> --order=k1,k2,k3 | delete <trip> <var> <destKey>
options add <trip> <var> <destKey> <kind> --from-json <file> [--free-cancellation-until <date>] [--details <json>] [--photos <json>]
edit <trip> <var> <destKey> <kind> <optKey> [--from-json <file>] [--free-cancellation-until <date>] [--details <json>] [--photos <json>]
options add <trip> <var> <destKey> <kind> --from-json <file> [--free-cancellation-until <date>] [--details <json>] [--photos <json>] [--website <url>]
edit <trip> <var> <destKey> <kind> <optKey> [--from-json <file>] [--free-cancellation-until <date>] [--details <json>] [--photos <json>] [--website <url>]
select <trip> <var> <destKey> <kind> <optKey> | deselect <trip> <var> <destKey> <kind>
delete <trip> <var> <destKey> <kind> <optKey>
events add <trip> <var> --from-json <file> | edit <trip> <var> <eventKey> --from-json
Expand Down Expand Up @@ -91,7 +91,7 @@ options, and leave out anything equally true of all of them, or unknown.
| `dinner`, `bedLayout`, `skiBackNote`, `mainLiftName`, `skiStorage`, `kidsFacilities`, `wellness`, `pool`, `beach`, `parking`, `noise`, `view`, `airConditioning`, `laundry`, `accessibility`, `pets`, `sourceNote` | text, up to 300 characters |

On an edit, only the `stayDetails` properties you send change. `null` clears one, and
`--details null` clears them all. `--photos` replaces the whole list, up to 12 photos, and
`--website` is the place's own official site, never a booking or listing site; leave it out if you can't confirm it. Photos from it may then be linked automatically, credited to the site. `--website null` clears it. `--details null` clears them all. `--photos` replaces the whole list, up to 12 photos, and
`--photos null` clears it. Unknown properties are rejected.

`photos` take `{ "url", "credit"?, "creditUrl"? }`. Each URL must be a directly linkable http or
Expand Down
7 changes: 6 additions & 1 deletion src/commands/options/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,19 @@ export const optionsAddCommand = defineCommand({
description:
'Accommodation only: photos as a JSON array of { "url", "credit"? }. Only images the owner may use.',
},
website: {
type: 'string',
description:
"Accommodation only: the place's own official website, not a booking site. Photos from it may be linked automatically, credited to the site.",
},
json: { type: 'boolean', default: false, description: 'Output as JSON.' },
},
async run({ args }) {
try {
assertKind(args.kind)
const body = await readJsonObject(args['from-json'])
applyFreeCancellation(body, args['free-cancellation-until'], args.kind)
applyStayFlags(body, { details: args.details, photos: args.photos }, args.kind)
applyStayFlags(body, { details: args.details, photos: args.photos, website: args.website }, args.kind)

const creds = await loadCredentials()
const apiKey = resolveApiKey(creds)
Expand Down
10 changes: 8 additions & 2 deletions src/commands/options/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ export const optionsEditCommand = defineCommand({
description:
'Accommodation only: photos as a JSON array of { "url", "credit"? }, replacing the list; null clears it. Only images the owner may use.',
},
website: {
type: 'string',
description:
"Accommodation only: the place's own official website, not a booking site. Photos from it may be linked automatically, credited to the site. null clears it.",
},
json: { type: 'boolean', default: false, description: 'Output as JSON.' },
},
async run({ args }) {
Expand All @@ -61,16 +66,17 @@ export const optionsEditCommand = defineCommand({
args['free-cancellation-until'],
args.details,
args.photos,
args.website,
].some((flag) => flag !== undefined)
if (!hasFlag) {
throw new Error(
'Specify at least one of --from-json, --free-cancellation-until, --details, --photos.',
'Specify at least one of --from-json, --free-cancellation-until, --details, --photos, --website.',
)
}
const body =
args['from-json'] !== undefined ? await readJsonObject(args['from-json']) : {}
applyFreeCancellation(body, args['free-cancellation-until'], args.kind)
applyStayFlags(body, { details: args.details, photos: args.photos }, args.kind)
applyStayFlags(body, { details: args.details, photos: args.photos, website: args.website }, args.kind)

const creds = await loadCredentials()
const apiKey = resolveApiKey(creds)
Expand Down
14 changes: 14 additions & 0 deletions src/commands/options/stay-details.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ describe('applyStayFlags', () => {
expect(body).toEqual({ name: 'Villa' })
})

test('sets and clears the website', () => {
const body: Record<string, unknown> = {}
applyStayFlags(body, { website: 'https://hotel.example/' }, 'accommodation')
expect(body.website).toBe('https://hotel.example/')

applyStayFlags(body, { website: 'null' }, 'accommodation')
expect(body.website).toBeNull()
})

test('refuses a website that is not a web address, and other kinds', () => {
expect(() => applyStayFlags({}, { website: 'javascript:alert(1)' }, 'accommodation')).toThrow('--website')
expect(() => applyStayFlags({}, { website: 'https://hotel.example/' }, 'transport')).toThrow('accommodation')
})

test('sets stayDetails and photos', () => {
const body: Record<string, unknown> = {}
applyStayFlags(
Expand Down
16 changes: 11 additions & 5 deletions src/commands/options/stay-details.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { parseJsonFlag, parseJsonObjectFlag } from '../../util/json-flag'

/**
* Merge the `--details` and `--photos` flags into an option body. Both fields
* exist only on accommodation, so any other kind is rejected. A flag overrides
* Merge the `--details`, `--photos` and `--website` flags into an option body. These
* fields exist only on accommodation, so any other kind is rejected. A flag overrides
* the same field in the `--from-json` file.
*/
export function applyStayFlags(
body: Record<string, unknown>,
flags: { details?: string; photos?: string },
flags: { details?: string; photos?: string; website?: string },
kind: string,
): void {
if (flags.details === undefined && flags.photos === undefined) return
if (flags.details === undefined && flags.photos === undefined && flags.website === undefined) return
if (kind !== 'accommodation') {
throw new Error('--details and --photos are only valid for the "accommodation" kind.')
throw new Error('--details, --photos and --website are only valid for the "accommodation" kind.')
}

if (flags.details !== undefined) {
Expand All @@ -27,4 +27,10 @@ export function applyStayFlags(
}
body.photos = photos
}
if (flags.website !== undefined) {
if (flags.website !== 'null' && !/^https?:\/\/\S+$/.test(flags.website)) {
throw new Error('--website must be an http or https URL, or null to clear it.')
}
body.website = flags.website === 'null' ? null : flags.website
}
}
Loading