mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
fix(backend): delete follow relations for deleted users (#8805)
* delete follows * migration-deleted-user-follows --------- Co-authored-by: Wolfgang Huß <wolle.huss@pjannto.com>
This commit is contained in:
parent
1612d03b52
commit
04dec08d04
@ -0,0 +1,51 @@
|
|||||||
|
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
||||||
|
|
||||||
|
import { getDriver } from '@db/neo4j'
|
||||||
|
|
||||||
|
export const description = 'Delete follow relation for deleted users'
|
||||||
|
|
||||||
|
export async function up(_next) {
|
||||||
|
const driver = getDriver()
|
||||||
|
const session = driver.session()
|
||||||
|
const transaction = session.beginTransaction()
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Implement your migration here.
|
||||||
|
await transaction.run(`
|
||||||
|
MATCH (:User {deleted: true})-[follow:FOLLOWS]-(:User)
|
||||||
|
DELETE follow;
|
||||||
|
`)
|
||||||
|
await transaction.commit()
|
||||||
|
} catch (error) {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.log(error)
|
||||||
|
await transaction.rollback()
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.log('rolled back')
|
||||||
|
throw new Error(error)
|
||||||
|
} finally {
|
||||||
|
await session.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function down(_next) {
|
||||||
|
const driver = getDriver()
|
||||||
|
const session = driver.session()
|
||||||
|
const transaction = session.beginTransaction()
|
||||||
|
|
||||||
|
try {
|
||||||
|
// cannot be rolled back
|
||||||
|
// Implement your migration here.
|
||||||
|
// await transaction.run(``)
|
||||||
|
// await transaction.commit()
|
||||||
|
} catch (error) {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.log(error)
|
||||||
|
await transaction.rollback()
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.log('rolled back')
|
||||||
|
throw new Error(error)
|
||||||
|
} finally {
|
||||||
|
await session.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -554,6 +554,40 @@ describe('Delete a User as admin', () => {
|
|||||||
await expect(database.neode.all('SocialMedia')).resolves.toHaveLength(0)
|
await expect(database.neode.all('SocialMedia')).resolves.toHaveLength(0)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('connected follow relations', () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
const userIFollow = await Factory.build('user', {
|
||||||
|
name: 'User I Follow',
|
||||||
|
about: 'I follow this user',
|
||||||
|
id: 'uifollow',
|
||||||
|
})
|
||||||
|
|
||||||
|
const userFollowingMe = await Factory.build('user', {
|
||||||
|
name: 'User Following Me',
|
||||||
|
about: 'This user follows me',
|
||||||
|
id: 'ufollowsme',
|
||||||
|
})
|
||||||
|
await user.relateTo(userIFollow, 'following')
|
||||||
|
await userFollowingMe.relateTo(user, 'following')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('will be removed completely', async () => {
|
||||||
|
const relation = await database.neode.cypher(
|
||||||
|
'MATCH (user:User {id: $id})-[relationship:FOLLOWS]-(:User) RETURN relationship',
|
||||||
|
{ id: (await user.toJson()).id },
|
||||||
|
)
|
||||||
|
const relations = relation.records.map((record) => record.get('relationship'))
|
||||||
|
expect(relations).toHaveLength(2)
|
||||||
|
await mutate({ mutation: deleteUserMutation, variables })
|
||||||
|
const relation2 = await database.neode.cypher(
|
||||||
|
'MATCH (user:User {id: $id})-[relationship:FOLLOWS]-(:User) RETURN relationship',
|
||||||
|
{ id: (await user.toJson()).id },
|
||||||
|
)
|
||||||
|
const relations2 = relation2.records.map((record) => record.get('relationship'))
|
||||||
|
expect(relations2).toHaveLength(0)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@ -280,6 +280,9 @@ export default {
|
|||||||
WITH user
|
WITH user
|
||||||
OPTIONAL MATCH (user)<-[:OWNED_BY]-(socialMedia:SocialMedia)
|
OPTIONAL MATCH (user)<-[:OWNED_BY]-(socialMedia:SocialMedia)
|
||||||
DETACH DELETE socialMedia
|
DETACH DELETE socialMedia
|
||||||
|
WITH user
|
||||||
|
OPTIONAL MATCH (user)-[follow:FOLLOWS]-(:User)
|
||||||
|
DELETE follow
|
||||||
RETURN user {.*}
|
RETURN user {.*}
|
||||||
`,
|
`,
|
||||||
{ userId },
|
{ userId },
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user