Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { Entity } from '../entity.js';
import type { RequestOptions } from '../request.js';
import type { Domain } from './domain.js';
import type { Job } from './job.js';
import type { Product } from './product.js';
/** Create payload; the API expects ``jobId`` — pass ``job`` with at least ``id``. */
export interface ProductReviewCreateInput {
job: { id: number };
rating: number;
title?: string | null;
content?: string | null;
}
export interface ProductReviewPatchInput {
status?: string;
rejectionReason?: string | null;
rating?: number;
title?: string | null;
content?: string | null;
}
/**
* Post-purchase product review. List/create are nested under ``/products/:id/reviews/``;
* updates use ``/product-reviews/:id/``. Do not use :meth:`Entity.create` / :meth:`Entity.list`
* for this resource.
*
* API responses use ``jobId``, ``productId``, and ``domainId``; this entity maps those onto
* ``job``, ``product``, and ``domain`` (minimal ``{ id }`` objects unless the API nests more).
*/
export class ProductReview extends Entity {
protected static resourceName = 'product-reviews';
protected static singularName = 'productReview';
protected static pluralName = 'productReviews';
@ProductReview.property()
public id?: number;
@ProductReview.property({ type: 'Product' })
public product?: Product;
@ProductReview.property({ type: 'Domain' })
public domain?: Domain;
@ProductReview.property({ type: Number })
public authorUserId?: number;
@ProductReview.property({ type: String })
public authorName?: string | null;
@ProductReview.property({ type: 'Job' })
public job?: Job;
@ProductReview.property({ type: Number })
public rating?: number;
@ProductReview.property({ type: String })
public title?: string | null;
@ProductReview.property({ type: String })
public content?: string | null;
@ProductReview.property({ type: String })
public status?: string;
@ProductReview.property({ type: Number })
public purchaseInvoiceId?: number | null;
/** ISO 8601 string from the API. */
@ProductReview.property({ type: String })
public submittedAt?: string | null;
/** ISO 8601 string from the API. */
@ProductReview.property({ type: String })
public updatedAt?: string | null;
@ProductReview.property({ type: String })
public rejectionReason?: string | null;
/**
* Map flat API fields (``jobId``, ``productId``, ``domainId``) to nested
* ``job`` / ``product`` / ``domain`` objects before :meth:`Entity.fromJson`.
*/
public static normalizeApiJson(json: any): any {
Iif (json === null || json === undefined || typeof json !== 'object') {
return json;
}
const row: any = { ...json };
Iif (row.jobId !== undefined && row.job === undefined) {
Iif (row.jobId != null) {
row.job = { id: row.jobId };
}
delete row.jobId;
}
Iif (row.productId !== undefined && row.product === undefined) {
Iif (row.productId != null) {
row.product = { id: row.productId };
}
delete row.productId;
}
Iif (row.domainId !== undefined && row.domain === undefined) {
Iif (row.domainId != null) {
row.domain = { id: row.domainId };
}
delete row.domainId;
}
return row;
}
/**
* Domain managers/admins: list reviews for a product.
*/
public static listForProduct(
this: typeof ProductReview,
productId: number,
options?: { withRights?: boolean }
): Promise<ProductReview[]> {
const Constructor = this;
const resource = `/products/${productId}/reviews/`;
const fetchOptions: RequestOptions = { method: 'GET' };
fetchOptions.query = [];
Iif (!(options && options.withRights)) {
fetchOptions.query.push(['skip_rights', 'y']);
}
return Constructor.merchi.authenticatedFetch(resource, fetchOptions).then(
(data: any) => {
const raw = data.productReviews ?? [];
return raw.map((row: any) => {
const e: ProductReview = new Constructor() as ProductReview;
e.fromJson(ProductReview.normalizeApiJson(row));
return e;
});
}
);
}
/**
* Buyer: create a review for the catalog ``productId`` (master product).
* ``body.job.id`` must be a completed job whose line item resolves to this product.
*/
public static createForProduct(
this: typeof ProductReview,
productId: number,
body: ProductReviewCreateInput,
options?: { withRights?: boolean }
): Promise<ProductReview> {
const Constructor = this;
const resource = `/products/${productId}/reviews/`;
const wire: Record<string, unknown> = {
jobId: body.job.id,
rating: body.rating,
};
Iif (body.title !== undefined) {
wire.title = body.title;
}
Iif (body.content !== undefined) {
wire.content = body.content;
}
const fetchOptions: RequestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(wire),
};
fetchOptions.query = [];
Iif (!(options && options.withRights)) {
fetchOptions.query.push(['skip_rights', 'y']);
}
return Constructor.merchi.authenticatedFetch(resource, fetchOptions).then(
(data: any) => {
const e: ProductReview = new Constructor() as ProductReview;
e.fromJson(ProductReview.normalizeApiJson(data.productReview));
return e;
}
);
}
/**
* Partial update (moderation or author edit). Uses ``multipart/form-data`` fields
* matching the Merchi API.
*/
public patch(
payload: ProductReviewPatchInput,
options?: { withRights?: boolean }
): Promise<this> {
const id = this.id;
Iif (id === undefined) {
throw new Error('ProductReview.id is required to patch');
}
const resource = `/product-reviews/${String(id)}/`;
const form = new FormData();
Iif (payload.status !== undefined) {
form.set('status', payload.status);
}
Iif (payload.rejectionReason !== undefined) {
form.set('rejectionReason', payload.rejectionReason ?? '');
}
Iif (payload.rating !== undefined) {
form.set('rating', String(payload.rating));
}
Iif (payload.title !== undefined) {
form.set('title', payload.title ?? '');
}
Iif (payload.content !== undefined) {
form.set('content', payload.content ?? '');
}
const fetchOptions: RequestOptions = { method: 'PATCH', body: form };
fetchOptions.query = [];
Iif (!(options && options.withRights)) {
fetchOptions.query.push(['skip_rights', 'y']);
}
const singularName = (this.constructor as typeof ProductReview)
.singularName;
return this.merchi.authenticatedFetch(resource, fetchOptions).then(
(data: any) => {
this.fromJson(ProductReview.normalizeApiJson(data[singularName]));
this.cleanDirty();
return this;
}
);
}
}
|