diff --git a/backend/src/middleware/permissionsMiddleware.ts b/backend/src/middleware/permissionsMiddleware.ts index 6cd8f39d6..6498bba2f 100644 --- a/backend/src/middleware/permissionsMiddleware.ts +++ b/backend/src/middleware/permissionsMiddleware.ts @@ -459,6 +459,7 @@ export default shield( switchUserRole: isAdmin, markTeaserAsViewed: allow, saveCategorySettings: isAuthenticated, + CreateRoom: isAuthenticated, }, User: { email: or(isMyOwn, isAdmin), diff --git a/backend/src/schema/resolvers/rooms.ts b/backend/src/schema/resolvers/rooms.ts new file mode 100644 index 000000000..26a5e7009 --- /dev/null +++ b/backend/src/schema/resolvers/rooms.ts @@ -0,0 +1,34 @@ +export default { + Mutation: { + CreateRoom: async (_parent, params, context, _resolveInfo) => { + const { userId } = params + const { user: { id: currentUserId } } = context + const session = context.driver.session() + const writeTxResultPromise = session.writeTransaction(async (transaction) => { + const createRoomCypher = ` + MATCH (currentUser:User { id: $currentUserId }) + MATCH (user:User { id: $userId }) + MERGE (currentUser)-[:CHATS_IN]->(room:Room)<-[:CHATS_IN]-(user) + SET room.createdAt = toString(datetime()) + RETURN room + ` + const createRommTxResponse = await await transaction.run( + createRoomCypher, + { userId, currentUserId } + ) + const [room] = await createRommTxResponse.records.map((record) => + record.get('room'), + ) + return room + }) + try { + const room = await writeTxResultPromise + return room + } catch (error) { + throw new Error(error) + } finally { + session.close() + } + } + } +} diff --git a/backend/src/schema/types/type/Room.gql b/backend/src/schema/types/type/Room.gql new file mode 100644 index 000000000..0127021cb --- /dev/null +++ b/backend/src/schema/types/type/Room.gql @@ -0,0 +1,13 @@ +type Room { + id: ID! + createdAt: String + updatedAt: String + + users: [User!]! @relation(name: "CHATS_IN", direction: "IN") +} + +type Mutation { + CreateRoom( + userId: ID! + ): Room +}