Have you inspected the charge port to make sure that it is OK or tried a different, known working, compatible charger or charge cable, you didn't say?
Has the phone been dropped or gotten wet at all?
Here's the Galaxy M31s charge troubleshooting flowchart from the service manual. Below that are some images showing the systemboard location of the components mentioned in the flowchart. The image on the left is zoomed in so that you can identify the component more clearly and the image on the right is of the entire board so as to provide its location on the board.
(click on image to enlarge.)
If it shows to replace the charge board at the 1 step in the flowchart, search online for Galaxy M31s charge port board, to find suppliers that suit you best.
Here's a video that shows how to replace the charge port board that may help.