export class CollectionService extends TrashableResourceService<CollectionResource> {
constructor(serverApi: AxiosInstance, private webdavClient: WebDAV, private authService: AuthService, actions: ApiActions) {
- super(serverApi, "collections", actions);
+ super(serverApi, "collections", actions, [
+ 'unsignedManifestText',
+ 'storageClassesConfirmed',
+ 'storageClassesConfirmedAt'
+ ]);
}
async files(uuid: string) {
expect(axiosInstance.post).toHaveBeenCalledWith("/resource", {owner_uuid: "ownerUuidValue"});
});
+ it("#create ignores fields listed as readonly", async () => {
+ axiosInstance.post = jest.fn(() => Promise.resolve({data: {}}));
+ const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
+ // UUID fields are read-only on all resources.
+ await commonResourceService.create({ uuid: "this should be ignored", ownerUuid: "ownerUuidValue" });
+ expect(axiosInstance.post).toHaveBeenCalledWith("/resource", {owner_uuid: "ownerUuidValue"});
+ });
+
+ it("#update ignores fields listed as readonly", async () => {
+ axiosInstance.put = jest.fn(() => Promise.resolve({data: {}}));
+ const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
+ // UUID fields are read-only on all resources.
+ await commonResourceService.update('resource-uuid', { uuid: "this should be ignored", ownerUuid: "ownerUuidValue" });
+ expect(axiosInstance.put).toHaveBeenCalledWith("/resource/resource-uuid", {owner_uuid: "ownerUuidValue"});
+ });
+
it("#delete", async () => {
axiosMock
.onDelete("/resource/uuid")
}
export class CommonResourceService<T extends Resource> extends CommonService<T> {
- constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions) {
- super(serverApi, resourceType, actions);
+ constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions, readOnlyFields: string[] = []) {
+ super(serverApi, resourceType, actions, readOnlyFields.concat([
+ 'uuid',
+ 'etag',
+ 'kind'
+ ]));
+ }
+
+ create(data?: Partial<T>) {
+ if (data !== undefined) {
+ this.readOnlyFields.forEach( field => delete data[field] );
+ }
+ return super.create(data);
+ }
+
+ update(uuid: string, data: Partial<T>) {
+ if (data !== undefined) {
+ this.readOnlyFields.forEach( field => delete data[field] );
+ }
+ return super.update(uuid, data);
}
}
protected serverApi: AxiosInstance;
protected resourceType: string;
protected actions: ApiActions;
+ protected readOnlyFields: string[];
- constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions) {
+ constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions, readOnlyFields: string[] = []) {
this.serverApi = serverApi;
this.resourceType = '/' + resourceType;
this.actions = actions;
+ this.readOnlyFields = readOnlyFields;
}
static mapResponseKeys = (response: { data: any }) =>
export class TrashableResourceService<T extends TrashableResource> extends CommonResourceService<T> {
- constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions) {
- super(serverApi, resourceType, actions);
+ constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions, readOnlyFields: string[] = []) {
+ super(serverApi, resourceType, actions, readOnlyFields);
}
trash(uuid: string): Promise<T> {
async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
dispatch(startSubmit(COLLECTION_COPY_FORM_NAME));
try {
- dispatch(progressIndicatorActions.START_WORKING(COLLECTION_COPY_FORM_NAME));
const collection = await services.collectionService.get(resource.uuid);
- const uuidKey = 'uuid';
- delete collection[uuidKey];
const newCollection = await services.collectionService.create({ ...collection, ownerUuid: resource.ownerUuid, name: resource.name });
dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_COPY_FORM_NAME }));
- dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_COPY_FORM_NAME));
return newCollection;
} catch (e) {
const error = getCommonResourceServiceError(e);
dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_COPY_FORM_NAME }));
throw new Error('Could not copy the collection.');
}
- dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_COPY_FORM_NAME));
return;
+ } finally {
+ dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_COPY_FORM_NAME));
}
};