issues-95 get saving and forking to work
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { createUserInsecure } from 'src/services/users/users.js'
|
||||
import { db } from 'src/lib/db'
|
||||
import { enforceAlphaNumeric } from 'src/services/helpers'
|
||||
import { enforceAlphaNumeric, generateUniqueString } from 'src/services/helpers'
|
||||
|
||||
export const handler = async (req, _context) => {
|
||||
const body = JSON.parse(req.body)
|
||||
@@ -58,26 +58,12 @@ export const handler = async (req, _context) => {
|
||||
|
||||
if (eventType === 'signup') {
|
||||
roles.push('user')
|
||||
// const hi = {
|
||||
// email: 'kurt.hutten@gmail.com',
|
||||
// image: '',
|
||||
// bio: ''
|
||||
// }
|
||||
|
||||
const generateUniqueUserName = async (seed, count = 0) => {
|
||||
const isUnique = !(await db.user.findOne({
|
||||
const isUniqueCallback = async (seed) =>
|
||||
db.user.findOne({
|
||||
where: { userName: seed },
|
||||
}))
|
||||
if (isUnique) {
|
||||
return seed
|
||||
}
|
||||
count += 1
|
||||
const newSeed =
|
||||
count === 1 ? `${seed}_${count}` : seed.slice(0, -1) + count
|
||||
return generateUniqueUserName(newSeed, count)
|
||||
}
|
||||
})
|
||||
const userNameSeed = enforceAlphaNumeric(email.split('@')[0])
|
||||
const userName = await generateUniqueUserName(userNameSeed) // TODO maybe come up with a better default userName?
|
||||
const userName = await generateUniqueString(userNameSeed, isUniqueCallback) // TODO maybe come up with a better default userName?
|
||||
const input = {
|
||||
email,
|
||||
userName,
|
||||
|
||||
@@ -37,6 +37,7 @@ export const schema = gql`
|
||||
|
||||
type Mutation {
|
||||
createPart(input: CreatePartInput!): Part!
|
||||
forkPart(input: CreatePartInput!): Part!
|
||||
updatePart(id: String!, input: UpdatePartInput!): Part!
|
||||
deletePart(id: String!): Part!
|
||||
}
|
||||
|
||||
@@ -14,3 +14,17 @@ export const foreignKeyReplacement = (input) => {
|
||||
|
||||
export const enforceAlphaNumeric = (string) =>
|
||||
string.replace(/([^a-zA-Z\d_:])/g, '-')
|
||||
|
||||
export const generateUniqueString = async (
|
||||
seed,
|
||||
isUniqueCallback,
|
||||
count = 0
|
||||
) => {
|
||||
const isUnique = !(await isUniqueCallback(seed))
|
||||
if (isUnique) {
|
||||
return seed
|
||||
}
|
||||
count += 1
|
||||
const newSeed = count === 1 ? `${seed}_${count}` : seed.slice(0, -1) + count
|
||||
return generateUniqueString(newSeed, isUniqueCallback, count)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { db } from 'src/lib/db'
|
||||
import {
|
||||
foreignKeyReplacement,
|
||||
enforceAlphaNumeric,
|
||||
generateUniqueString,
|
||||
} from 'src/services/helpers'
|
||||
import { requireAuth } from 'src/lib/auth'
|
||||
import { requireOwnership } from 'src/lib/owner'
|
||||
@@ -38,6 +39,25 @@ export const createPart = async ({ input }) => {
|
||||
})
|
||||
}
|
||||
|
||||
export const forkPart = async ({ input }) => {
|
||||
// Only difference between create nda clone part is that clone part will generate a unique title
|
||||
// (for the user) if there is a conflict
|
||||
const isUniqueCallback = async (seed) =>
|
||||
db.part.findOne({
|
||||
where: {
|
||||
title_userId: {
|
||||
title: seed,
|
||||
userId: input.userId,
|
||||
},
|
||||
},
|
||||
})
|
||||
const title = await generateUniqueString(input.title, isUniqueCallback)
|
||||
// TODO change the description to `forked from userName/partName ${rest of description}`
|
||||
return db.part.create({
|
||||
data: foreignKeyReplacement({ ...input, title }),
|
||||
})
|
||||
}
|
||||
|
||||
export const updatePart = async ({ id, input }) => {
|
||||
requireAuth()
|
||||
await requireOwnership({ partId: id })
|
||||
|
||||
Reference in New Issue
Block a user