|
| 1 | +<!-- |
| 2 | + - SPDX-FileCopyrightText: 2026 Conduction B.V. <info@conduction.nl> |
| 3 | + - SPDX-License-Identifier: EUPL-1.2 |
| 4 | + --> |
| 5 | +<template> |
| 6 | + <CnWidgetWrapper |
| 7 | + :title="t('stackiq', 'Supplier')" |
| 8 | + titleIconPosition="left" |
| 9 | + :showRefresh="false" |
| 10 | + :showRequestFeature="false"> |
| 11 | + <template #title-icon> |
| 12 | + <CnIcon name="OfficeBuildingOutline" :size="20" /> |
| 13 | + </template> |
| 14 | + <section class="contract-supplier-panel" data-testid="contract-supplier-panel"> |
| 15 | + <NcLoadingIcon |
| 16 | + v-if="loading" |
| 17 | + :size="32" |
| 18 | + :name="t('stackiq', 'Loading the supplier')" /> |
| 19 | + <NcNoteCard v-else-if="error" type="error"> |
| 20 | + {{ t('stackiq', 'The supplier could not be loaded.') }} |
| 21 | + </NcNoteCard> |
| 22 | + <p |
| 23 | + v-else-if="!supplier" |
| 24 | + class="contract-supplier-panel__empty" |
| 25 | + data-testid="contract-supplier-empty"> |
| 26 | + {{ emptyLabel }} |
| 27 | + </p> |
| 28 | + <dl v-else class="contract-supplier-panel__fields"> |
| 29 | + <div> |
| 30 | + <dt>{{ t('stackiq', 'Supplier') }}</dt> |
| 31 | + <dd data-testid="contract-supplier-name"> |
| 32 | + <router-link :to="supplierRoute"> |
| 33 | + {{ supplier.name }} |
| 34 | + </router-link> |
| 35 | + </dd> |
| 36 | + </div> |
| 37 | + <div v-if="service && service.name"> |
| 38 | + <dt>{{ t('stackiq', 'Through the service') }}</dt> |
| 39 | + <dd>{{ service.name }}</dd> |
| 40 | + </div> |
| 41 | + </dl> |
| 42 | + </section> |
| 43 | + </CnWidgetWrapper> |
| 44 | +</template> |
| 45 | + |
| 46 | +<script> |
| 47 | +/** |
| 48 | + * The supplier of a contract (REQ-CLR-014): a contract names the service it |
| 49 | + * is for, and that service names its provider, so the panel reads the three |
| 50 | + * records in turn and links the provider to its organisation page. |
| 51 | + */ |
| 52 | +import { CnIcon, CnWidgetWrapper } from '@conduction/nextcloud-vue' |
| 53 | +import axios from '@nextcloud/axios' |
| 54 | +import { translate as t } from '@nextcloud/l10n' |
| 55 | +import { generateUrl } from '@nextcloud/router' |
| 56 | +import { NcLoadingIcon, NcNoteCard } from '@nextcloud/vue' |
| 57 | +
|
| 58 | +/** |
| 59 | + * The object in an OpenRegister object reply: the body itself when it |
| 60 | + * carries `@self`, else its `object`, else the body, else an empty object. |
| 61 | + * |
| 62 | + * @param {object|null} data The reply body. |
| 63 | + * @return {object} The object. |
| 64 | + */ |
| 65 | +function objectOf(data) { |
| 66 | + return data && data['@self'] !== undefined ? data : data?.object || data || {} |
| 67 | +} |
| 68 | +
|
| 69 | +/** |
| 70 | + * The id a reference property holds: a uuid string, or the `id` (or |
| 71 | + * `@self.id`) of an expanded object. Empty when the property is unset. |
| 72 | + * |
| 73 | + * @param {string|object|null|undefined} value The property value. |
| 74 | + * @return {string} The referenced id, or ''. |
| 75 | + * @spec openspec/changes/capability-rows-the-code-does-not-yet-meet/specs/vendor-product-master-data/spec.md#requirement-a-contract-shows-its-supplier-through-the-service-it-is-for-req-clr-014 |
| 76 | + */ |
| 77 | +export function referenceId(value) { |
| 78 | + if (typeof value === 'string') { |
| 79 | + return value.trim() |
| 80 | + } |
| 81 | + if (value && typeof value === 'object') { |
| 82 | + const id = value.id ?? value['@self']?.id ?? value.uuid |
| 83 | + return id === undefined || id === null ? '' : String(id).trim() |
| 84 | + } |
| 85 | + return '' |
| 86 | +} |
| 87 | +
|
| 88 | +export default { |
| 89 | + name: 'ContractSupplierPanel', |
| 90 | + components: { |
| 91 | + CnIcon, |
| 92 | + CnWidgetWrapper, |
| 93 | + NcLoadingIcon, |
| 94 | + NcNoteCard, |
| 95 | + }, |
| 96 | +
|
| 97 | + props: { |
| 98 | + objectId: { |
| 99 | + type: [String, Number], |
| 100 | + default: '', |
| 101 | + }, |
| 102 | +
|
| 103 | + register: { |
| 104 | + type: String, |
| 105 | + default: 'stackiq', |
| 106 | + }, |
| 107 | +
|
| 108 | + schema: { |
| 109 | + type: String, |
| 110 | + default: 'catalogContract', |
| 111 | + }, |
| 112 | +
|
| 113 | + serviceSchema: { |
| 114 | + type: String, |
| 115 | + default: 'catalogService', |
| 116 | + }, |
| 117 | +
|
| 118 | + organisationSchema: { |
| 119 | + type: String, |
| 120 | + default: 'organization', |
| 121 | + }, |
| 122 | + }, |
| 123 | +
|
| 124 | + data() { |
| 125 | + return { |
| 126 | + loading: true, |
| 127 | + error: false, |
| 128 | + service: null, |
| 129 | + supplier: null, |
| 130 | + hasService: false, |
| 131 | + } |
| 132 | + }, |
| 133 | +
|
| 134 | + computed: { |
| 135 | + /** |
| 136 | + * The supplier's organisation page. |
| 137 | + * |
| 138 | + * @return {object} The route. |
| 139 | + * @spec openspec/changes/capability-rows-the-code-does-not-yet-meet/specs/vendor-product-master-data/spec.md#requirement-a-contract-shows-its-supplier-through-the-service-it-is-for-req-clr-014 |
| 140 | + */ |
| 141 | + supplierRoute() { |
| 142 | + return { name: 'OrganisatieDetail', params: { id: String(this.supplier?.id ?? '') } } |
| 143 | + }, |
| 144 | +
|
| 145 | + /** |
| 146 | + * Why no supplier shows: the contract names no service, or its service no provider. |
| 147 | + * |
| 148 | + * @return {string} The translated line. |
| 149 | + * @spec openspec/changes/capability-rows-the-code-does-not-yet-meet/specs/vendor-product-master-data/spec.md#requirement-a-contract-shows-its-supplier-through-the-service-it-is-for-req-clr-014 |
| 150 | + */ |
| 151 | + emptyLabel() { |
| 152 | + return this.hasService |
| 153 | + ? t('stackiq', 'The service of this contract names no supplier.') |
| 154 | + : t('stackiq', 'This contract names no service, so its supplier is not known.') |
| 155 | + }, |
| 156 | + }, |
| 157 | +
|
| 158 | + async mounted() { |
| 159 | + await this.load() |
| 160 | + }, |
| 161 | +
|
| 162 | + methods: { |
| 163 | + t, |
| 164 | +
|
| 165 | + /** |
| 166 | + * Read the contract, then its service, then the service's provider. |
| 167 | + * |
| 168 | + * @return {Promise<void>} Resolves once the supplier is read or known to be absent. |
| 169 | + * @spec openspec/changes/capability-rows-the-code-does-not-yet-meet/specs/vendor-product-master-data/spec.md#requirement-a-contract-shows-its-supplier-through-the-service-it-is-for-req-clr-014 |
| 170 | + */ |
| 171 | + async load() { |
| 172 | + this.loading = true |
| 173 | + this.error = false |
| 174 | + this.service = null |
| 175 | + this.supplier = null |
| 176 | + this.hasService = false |
| 177 | + try { |
| 178 | + if (!this.objectId) { |
| 179 | + return |
| 180 | + } |
| 181 | + const contract = await this.fetchObject(this.schema, String(this.objectId)) |
| 182 | + const serviceId = referenceId(contract.service) |
| 183 | + if (serviceId === '') { |
| 184 | + return |
| 185 | + } |
| 186 | + this.hasService = true |
| 187 | + const service = await this.fetchObject(this.serviceSchema, serviceId) |
| 188 | + this.service = { id: serviceId, name: service.name || '' } |
| 189 | + const providerId = referenceId(service.provider) |
| 190 | + if (providerId === '') { |
| 191 | + return |
| 192 | + } |
| 193 | + const provider = await this.fetchObject(this.organisationSchema, providerId) |
| 194 | + this.supplier = { id: providerId, name: provider.name || provider.title || providerId } |
| 195 | + } catch { |
| 196 | + this.error = true |
| 197 | + } finally { |
| 198 | + this.loading = false |
| 199 | + } |
| 200 | + }, |
| 201 | +
|
| 202 | + /** |
| 203 | + * Fetch one object of this register, unwrapped from its envelope. |
| 204 | + * |
| 205 | + * @param {string} schema The schema slug. |
| 206 | + * @param {string} id The object id. |
| 207 | + * @return {Promise<object>} The object. |
| 208 | + * @spec openspec/changes/capability-rows-the-code-does-not-yet-meet/specs/vendor-product-master-data/spec.md#requirement-a-contract-shows-its-supplier-through-the-service-it-is-for-req-clr-014 |
| 209 | + */ |
| 210 | + async fetchObject(schema, id) { |
| 211 | + const url = generateUrl( |
| 212 | + '/apps/openregister/api/objects/{register}/{schema}/{id}', |
| 213 | + { register: this.register, schema, id }, |
| 214 | + ) |
| 215 | + const { data } = await axios.get(url) |
| 216 | + return objectOf(data) |
| 217 | + }, |
| 218 | + }, |
| 219 | +} |
| 220 | +</script> |
| 221 | +
|
| 222 | +<style scoped> |
| 223 | +.contract-supplier-panel__fields { |
| 224 | + display: grid; |
| 225 | + grid-template-columns: repeat(2, minmax(0, 1fr)); |
| 226 | + gap: 8px; |
| 227 | + margin: 0; |
| 228 | +} |
| 229 | +
|
| 230 | +.contract-supplier-panel__fields dt, |
| 231 | +.contract-supplier-panel__empty { |
| 232 | + color: var(--color-text-maxcontrast); |
| 233 | +} |
| 234 | +
|
| 235 | +.contract-supplier-panel__fields dd { |
| 236 | + margin: 0; |
| 237 | +} |
| 238 | +</style> |
0 commit comments