feat: added ability to update and listen to updates of shared-requests
This commit is contained in:
@@ -629,15 +629,22 @@ export const MAILER_FROM_ADDRESS_UNDEFINED =
|
|||||||
export const SHARED_REQUEST_NOT_FOUND = 'shared_request/not_found' as const;
|
export const SHARED_REQUEST_NOT_FOUND = 'shared_request/not_found' as const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SharedRequest invalid request JSON formal
|
* SharedRequest invalid request JSON format
|
||||||
* (SharedRequestService)
|
* (SharedRequestService)
|
||||||
*/
|
*/
|
||||||
export const SHARED_REQUEST_INVALID_REQUEST_JSON =
|
export const SHARED_REQUEST_INVALID_REQUEST_JSON =
|
||||||
'shared_request/request_invalid_format' as const;
|
'shared_request/request_invalid_format' as const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SharedRequest invalid properties JSON formal
|
* SharedRequest invalid properties JSON format
|
||||||
* (SharedRequestService)
|
* (SharedRequestService)
|
||||||
*/
|
*/
|
||||||
export const SHARED_REQUEST_INVALID_PROPERTIES_JSON =
|
export const SHARED_REQUEST_INVALID_PROPERTIES_JSON =
|
||||||
'shared_request/properties_invalid_format' as const;
|
'shared_request/properties_invalid_format' as const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SharedRequest invalid properties not found
|
||||||
|
* (SharedRequestService)
|
||||||
|
*/
|
||||||
|
export const SHARED_REQUEST_PROPERTIES_NOT_FOUND =
|
||||||
|
'shared_request/properties_not_found' as const;
|
||||||
|
|||||||
@@ -71,5 +71,7 @@ export type TopicDef = {
|
|||||||
[topic: `team/${string}/invite_added`]: TeamInvitation;
|
[topic: `team/${string}/invite_added`]: TeamInvitation;
|
||||||
[topic: `team/${string}/invite_removed`]: string;
|
[topic: `team/${string}/invite_removed`]: string;
|
||||||
[topic: `shortcode/${string}/${'created' | 'revoked'}`]: Shortcode;
|
[topic: `shortcode/${string}/${'created' | 'revoked'}`]: Shortcode;
|
||||||
[topic: `shared_request/${string}/${'created' | 'revoked'}`]: SharedRequest;
|
[
|
||||||
|
topic: `shared_request/${string}/${'created' | 'revoked' | 'updated'}`
|
||||||
|
]: SharedRequest;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -85,6 +85,34 @@ export class SharedRequestResolver {
|
|||||||
return result.right;
|
return result.right;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Mutation(() => SharedRequest, {
|
||||||
|
description: 'Update a user generated shared-request',
|
||||||
|
})
|
||||||
|
@UseGuards(GqlAuthGuard)
|
||||||
|
async updateSharedRequest(
|
||||||
|
@GqlUser() user: AuthUser,
|
||||||
|
@Args({
|
||||||
|
name: 'code',
|
||||||
|
type: () => ID,
|
||||||
|
description: 'The shared-request to update',
|
||||||
|
})
|
||||||
|
code: string,
|
||||||
|
@Args({
|
||||||
|
name: 'properties',
|
||||||
|
description: 'JSON string of the properties of the embed',
|
||||||
|
})
|
||||||
|
properties: string,
|
||||||
|
) {
|
||||||
|
const result = await this.sharedRequestService.updateSharedRequest(
|
||||||
|
code,
|
||||||
|
user.uid,
|
||||||
|
properties,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (E.isLeft(result)) throwErr(result.left);
|
||||||
|
return result.right;
|
||||||
|
}
|
||||||
|
|
||||||
@Mutation(() => Boolean, {
|
@Mutation(() => Boolean, {
|
||||||
description: 'Revoke a user generated shared-request',
|
description: 'Revoke a user generated shared-request',
|
||||||
})
|
})
|
||||||
@@ -118,6 +146,16 @@ export class SharedRequestResolver {
|
|||||||
return this.pubsub.asyncIterator(`shared_request/${user.uid}/created`);
|
return this.pubsub.asyncIterator(`shared_request/${user.uid}/created`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Subscription(() => SharedRequest, {
|
||||||
|
description: 'Listen for shared-request updates',
|
||||||
|
resolve: (value) => value,
|
||||||
|
})
|
||||||
|
@SkipThrottle()
|
||||||
|
@UseGuards(GqlAuthGuard)
|
||||||
|
mySharedRequestUpdated(@GqlUser() user: AuthUser) {
|
||||||
|
return this.pubsub.asyncIterator(`shared_request/${user.uid}/updated`);
|
||||||
|
}
|
||||||
|
|
||||||
@Subscription(() => SharedRequest, {
|
@Subscription(() => SharedRequest, {
|
||||||
description: 'Listen for shared-request deletion',
|
description: 'Listen for shared-request deletion',
|
||||||
resolve: (value) => value,
|
resolve: (value) => value,
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
SHARED_REQUEST_INVALID_PROPERTIES_JSON,
|
SHARED_REQUEST_INVALID_PROPERTIES_JSON,
|
||||||
SHARED_REQUEST_INVALID_REQUEST_JSON,
|
SHARED_REQUEST_INVALID_REQUEST_JSON,
|
||||||
SHARED_REQUEST_NOT_FOUND,
|
SHARED_REQUEST_NOT_FOUND,
|
||||||
|
SHARED_REQUEST_PROPERTIES_NOT_FOUND,
|
||||||
} from 'src/errors';
|
} from 'src/errors';
|
||||||
import { stringToJson } from 'src/utils';
|
import { stringToJson } from 'src/utils';
|
||||||
import { AuthUser } from 'src/types/AuthUser';
|
import { AuthUser } from 'src/types/AuthUser';
|
||||||
@@ -135,13 +136,6 @@ export class SharedRequestService implements UserDataHandler, OnModuleInit {
|
|||||||
if (E.isLeft(requestData))
|
if (E.isLeft(requestData))
|
||||||
return E.left(SHARED_REQUEST_INVALID_REQUEST_JSON);
|
return E.left(SHARED_REQUEST_INVALID_REQUEST_JSON);
|
||||||
|
|
||||||
// let propertiesData;
|
|
||||||
// if (!properties) propertiesData = undefined;
|
|
||||||
// const parsedProperties = stringToJson(properties);
|
|
||||||
// if (E.isLeft(parsedProperties))
|
|
||||||
// return E.left(SHARED_REQUEST_INVALID_PROPERTIES_JSON);
|
|
||||||
// propertiesData = parsedProperties.right;
|
|
||||||
|
|
||||||
const parsedProperties = stringToJson(properties);
|
const parsedProperties = stringToJson(properties);
|
||||||
if (E.isLeft(parsedProperties))
|
if (E.isLeft(parsedProperties))
|
||||||
return E.left(SHARED_REQUEST_INVALID_PROPERTIES_JSON);
|
return E.left(SHARED_REQUEST_INVALID_PROPERTIES_JSON);
|
||||||
@@ -196,7 +190,7 @@ export class SharedRequestService implements UserDataHandler, OnModuleInit {
|
|||||||
/**
|
/**
|
||||||
* Delete a SharedRequest
|
* Delete a SharedRequest
|
||||||
*
|
*
|
||||||
* @param sharedRequestID SharedRequest
|
* @param sharedRequestID SharedRequest ID
|
||||||
* @param uid User Uid
|
* @param uid User Uid
|
||||||
* @returns Boolean on successful deletion
|
* @returns Boolean on successful deletion
|
||||||
*/
|
*/
|
||||||
@@ -221,4 +215,45 @@ export class SharedRequestService implements UserDataHandler, OnModuleInit {
|
|||||||
return E.left(SHARED_REQUEST_NOT_FOUND);
|
return E.left(SHARED_REQUEST_NOT_FOUND);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update a created SharedRequest
|
||||||
|
* @param sharedRequestID SharedRequest ID
|
||||||
|
* @param uid User Uid
|
||||||
|
* @returns Updated SharedRequest
|
||||||
|
*/
|
||||||
|
async updateSharedRequest(
|
||||||
|
sharedRequestID: string,
|
||||||
|
uid: string,
|
||||||
|
updatedProps: string,
|
||||||
|
) {
|
||||||
|
if (!updatedProps) return E.left(SHARED_REQUEST_PROPERTIES_NOT_FOUND);
|
||||||
|
|
||||||
|
const parsedProperties = stringToJson(updatedProps);
|
||||||
|
if (E.isLeft(parsedProperties))
|
||||||
|
return E.left(SHARED_REQUEST_INVALID_PROPERTIES_JSON);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const updatedSharedRequest = await this.prisma.shortcode.update({
|
||||||
|
where: {
|
||||||
|
creator_uid_shortcode_unique: {
|
||||||
|
creatorUid: uid,
|
||||||
|
id: sharedRequestID,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
properties: updatedProps,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
this.pubsub.publish(
|
||||||
|
`shared_request/${updatedSharedRequest.creatorUid}/updated`,
|
||||||
|
this.cast(updatedSharedRequest),
|
||||||
|
);
|
||||||
|
|
||||||
|
return E.right(this.cast(updatedSharedRequest));
|
||||||
|
} catch (error) {
|
||||||
|
return E.left(SHARED_REQUEST_NOT_FOUND);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user