Skip to content

Commit

Permalink
Merge pull request #39 from curiosta/feat/add-user-deletion
Browse files Browse the repository at this point in the history
Feat/add user deletion
  • Loading branch information
ShivamJoker authored Oct 6, 2023
2 parents 9b1a647 + fe9898b commit 902356d
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 53 deletions.
23 changes: 23 additions & 0 deletions datasource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { DataSource } = require("typeorm");
const dotenv = require("dotenv");

dotenv.config()

const AppDataSource = new DataSource({
type: "postgres",
port: 5432,
port: process.env.DB_PORT,
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
host: process.env.DB_HOST,
entities: [
],
migrations: [
"dist/migrations/*.js",
],
})

module.exports = {
datasource: AppDataSource,
}
Binary file modified dump.rdb
Binary file not shown.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
"seed": "medusa seed -f ./data/seed.json",
"start": "npm run build && medusa migrations run && medusa develop -p=8080",
"dev": "npm run build && medusa develop",
"build:admin": "medusa-admin build"
"build:admin": "medusa-admin build",
"migration:run": "npm run build && npx typeorm migration:run -d datasource.js",
"migration:generate": "npx typeorm migration:generate -d datasource.js"
},
"dependencies": {
"@babel/preset-typescript": "^7.21.5",
Expand Down Expand Up @@ -92,4 +94,4 @@
"engines": {
"node": ">=18.0.0"
}
}
}
9 changes: 5 additions & 4 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
} from "medusa-core-utils";
import { ConfigModule } from "@medusajs/medusa";
import cors from 'cors'
import { restrictUser } from "./routes/admin/restrict-user";
import { deleteCustomer } from "./routes/admin/delete-customer";
import { listDeletedCustomers } from "./routes/admin/list-deleted";
import { restoreCustomer } from "./routes/admin/restore-customer";

export default (rootDirectory: string): Router | Router[] => {
const { configModule: { projectConfig } } = getConfigFile<ConfigModule>(rootDirectory, "medusa-config")
Expand All @@ -16,9 +18,8 @@ export default (rootDirectory: string): Router | Router[] => {
const router = Router();
router.use(cors(storefrontCorsConfig))

const endpointHandlers = [restrictUser]


// endpoints
const endpointHandlers = [deleteCustomer, listDeletedCustomers, restoreCustomer]
endpointHandlers.forEach(endpointHandle => endpointHandle(router))

return router
Expand Down
17 changes: 17 additions & 0 deletions src/api/routes/admin/delete-customer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { CustomerService, authenticate } from "@medusajs/medusa"
import express, { Router } from "express";

export const deleteCustomer = (router: Router) => {
router.use('/admin/customers/delete', express.json(), authenticate());

router.post('/admin/customers/delete', async (req, res) => {
try {
const customerService = req.scope.resolve('customerService') as CustomerService;
const customer = await customerService.retrieveRegisteredByEmail(req.body.email)
await customerService.delete(customer.id)
return res.json({ status: 200, message: 'User has been deleted!' })
} catch (error) {
return res.status(500).json({ status: 500, message: 'An error occurred!', error: error instanceof Error ? error.message : error })
}
})
}
17 changes: 17 additions & 0 deletions src/api/routes/admin/list-deleted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { authenticate } from "@medusajs/medusa"
import { Router } from "express";
import CustomerService from "../../../services/customer";

export const listDeletedCustomers = (router: Router) => {
router.use('/admin/customers/list-deleted', authenticate());

router.get('/admin/customers/list-deleted', async (req, res) => {
const customerService = req.scope.resolve('customerService') as CustomerService
try {
const result = await customerService.retrieveDeletedCustomer()
return res.json(result)
} catch (error) {
return res.status(500).json({ status: 500, message: 'An error occurred!', error: error instanceof Error ? error.message : error })
}
})
}
21 changes: 21 additions & 0 deletions src/api/routes/admin/restore-customer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { authenticate } from "@medusajs/medusa"
import express, { Router } from "express";
import CustomerService from "../../../services/customer";

export const restoreCustomer = (router: Router) => {
router.use('/admin/customers/restore', express.json(), authenticate());

router.post('/admin/customers/restore', async (req, res) => {

if (!req.body.email) return res.status(422).json({ status: 422, message: 'Insufficient payload!' })

try {
const customerService = req.scope.resolve('customerService') as CustomerService
await customerService.restoreCustomer(req.body.email);

return res.json({ status: 200, message: 'User has been restored!' })
} catch (error) {
return res.status(500).json({ status: 500, message: 'An error occurred!', error: error instanceof Error ? error.message : error })
}
})
}
18 changes: 0 additions & 18 deletions src/api/routes/admin/restrict-user.ts

This file was deleted.

29 changes: 0 additions & 29 deletions src/migrations/README.md

This file was deleted.

7 changes: 7 additions & 0 deletions src/repositories/deleted-customer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {
dataSource,
} from "@medusajs/medusa/dist/loaders/database"
import { DeletedCustomer } from "../models/deleted-customer"

export const DeletedCustomerRepository = dataSource.getRepository(DeletedCustomer)
export default DeletedCustomerRepository
39 changes: 39 additions & 0 deletions src/services/customer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { CustomerService as BaseCustomerService, Customer } from '@medusajs/medusa'

export default class CustomerService extends BaseCustomerService {
constructor(container) {
super(container)
}


async retrieveDeletedCustomer() {
const deletedCustomerQuery = `
SELECT id, email, first_name, last_name, billing_address_id, phone, has_account, metadata, created_at, updated_at, deleted_at FROM customer WHERE deleted_at IS NOT NULL
`

const listOfDeletedCustomers = await this.activeManager_.query(deletedCustomerQuery)

return listOfDeletedCustomers
}

async restoreCustomer(email: string) {
// check if email exists!

const customerByEmailQuery = `
SELECT * FROM customer WHERE email = $1
`

const customerResult = await this.activeManager_.query(customerByEmailQuery, [email]) as Customer[]

if (!customerResult.length) {
console.log(customerResult);
throw new Error('Customer not found with this email!')
}

const restoreCustomerQuery = `
UPDATE customer SET deleted_at = NULL WHERE email = $1
`

await this.activeManager_.query(restoreCustomerQuery, [email])
}
}

0 comments on commit 902356d

Please sign in to comment.