MCPcopy Create free account
hub / github.com/zenstackhq/zenstack / processUpdate

Method processUpdate

packages/server/src/api/rest/index.ts:1721–1786  ·  view source on GitHub ↗
(
        client: ClientContract<Schema>,
        type: any,
        resourceId: string,
        _query: Record<string, string | string[]> | undefined,
        requestBody: unknown,
    )

Source from the content-addressed store, hash-verified

1719 }
1720
1721 private async processUpdate(
1722 client: ClientContract<Schema>,
1723 type: any,
1724 resourceId: string,
1725 _query: Record<string, string | string[]> | undefined,
1726 requestBody: unknown,
1727 ): Promise<Response> {
1728 const typeInfo = this.getModelInfo(type);
1729 if (!typeInfo) {
1730 return this.makeUnsupportedModelError(type);
1731 }
1732
1733 const { attributes, relationships, error } = this.processRequestBody(requestBody);
1734 if (error) {
1735 return error;
1736 }
1737
1738 const updatePayload: any = {
1739 where: this.makeIdFilter(typeInfo.idFields, resourceId),
1740 data: { ...attributes },
1741 };
1742
1743 // turn relationships into query payload
1744 if (relationships) {
1745 for (const [key, data] of Object.entries<any>(relationships)) {
1746 if (!data?.data) {
1747 return this.makeError('invalidRelationData');
1748 }
1749
1750 const relationInfo = typeInfo.relationships[key];
1751 if (!relationInfo) {
1752 return this.makeUnsupportedRelationshipError(type, key, 400);
1753 }
1754
1755 if (relationInfo.isCollection) {
1756 updatePayload.data[key] = {
1757 set: enumerate(data.data).map((item: any) => ({
1758 [this.makeDefaultIdKey(relationInfo.idFields)]: item.id,
1759 })),
1760 };
1761 } else {
1762 if (typeof data.data !== 'object') {
1763 return this.makeError('invalidRelationData');
1764 }
1765 updatePayload.data[key] = {
1766 connect: {
1767 [this.makeDefaultIdKey(relationInfo.idFields)]: data.data.id,
1768 },
1769 };
1770 }
1771 updatePayload.include = {
1772 ...updatePayload.include,
1773 [key]: { select: { [this.makeDefaultIdKey(relationInfo.idFields)]: true } },
1774 };
1775 }
1776 }
1777
1778 // include IDs of relation fields so that they can be serialized.

Callers 1

handleRequestMethod · 0.95

Calls 10

getModelInfoMethod · 0.95
processRequestBodyMethod · 0.95
makeIdFilterMethod · 0.95
makeErrorMethod · 0.95
makeDefaultIdKeyMethod · 0.95
serializeItemsMethod · 0.95
enumerateFunction · 0.90

Tested by

no test coverage detected