mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
create indexId when creating the message
This commit is contained in:
parent
0cce43500a
commit
70c310bc88
@ -18,26 +18,8 @@ export default {
|
|||||||
const resolved = await neo4jgraphql(object, params, context, resolveInfo)
|
const resolved = await neo4jgraphql(object, params, context, resolveInfo)
|
||||||
|
|
||||||
if (resolved) {
|
if (resolved) {
|
||||||
const session = context.driver.session()
|
|
||||||
const countMessageTxPromise = session.readTransaction(async (transaction) => {
|
|
||||||
const countMessageCypher = `
|
|
||||||
MATCH((message:Message)-[:INSIDE]->(:Room { id: $roomId })) RETURN COUNT(message) AS count
|
|
||||||
`
|
|
||||||
const countMessageTxResponse = await transaction.run(countMessageCypher, {
|
|
||||||
roomId,
|
|
||||||
})
|
|
||||||
return await countMessageTxResponse.records[0].get('count')
|
|
||||||
})
|
|
||||||
try {
|
|
||||||
const count = await countMessageTxPromise
|
|
||||||
for (let i = 0; i < resolved.length; i++) {
|
for (let i = 0; i < resolved.length; i++) {
|
||||||
resolved[i]._id = resolved[i].id
|
resolved[i]._id = resolved[i].id
|
||||||
resolved[i].indexId = count - 1 - params.offset - i
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
throw new Error(error)
|
|
||||||
} finally {
|
|
||||||
session.close()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return resolved
|
return resolved
|
||||||
@ -53,9 +35,12 @@ export default {
|
|||||||
const writeTxResultPromise = session.writeTransaction(async (transaction) => {
|
const writeTxResultPromise = session.writeTransaction(async (transaction) => {
|
||||||
const createMessageCypher = `
|
const createMessageCypher = `
|
||||||
MATCH (currentUser:User { id: $currentUserId })-[:CHATS_IN]->(room:Room { id: $roomId })
|
MATCH (currentUser:User { id: $currentUserId })-[:CHATS_IN]->(room:Room { id: $roomId })
|
||||||
|
OPTIONAL MATCH (m:Message)-[:INSIDE]->(room)
|
||||||
|
WITH MAX(m.indexId) as maxIndex, room, currentUser
|
||||||
CREATE (currentUser)-[:CREATED]->(message:Message {
|
CREATE (currentUser)-[:CREATED]->(message:Message {
|
||||||
createdAt: toString(datetime()),
|
createdAt: toString(datetime()),
|
||||||
id: apoc.create.uuid(),
|
id: apoc.create.uuid(),
|
||||||
|
indexId: CASE WHEN maxIndex IS NOT NULL THEN maxIndex + 1 ELSE 0 END,
|
||||||
content: $content
|
content: $content
|
||||||
})-[:INSIDE]->(room)
|
})-[:INSIDE]->(room)
|
||||||
RETURN message { .* }
|
RETURN message { .* }
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user